[wp-trac] [WordPress Trac] #11579: WordPress does not serialize properly multidimensional arrays in postmeta

WordPress Trac wp-trac at lists.automattic.com
Wed Dec 23 16:26:20 UTC 2009


#11579: WordPress does not serialize properly multidimensional arrays in postmeta
--------------------------+-------------------------------------------------
 Reporter:  ickatanet     |       Owner:  westi                                                                    
     Type:  defect (bug)  |      Status:  new                                                                      
 Priority:  normal        |   Milestone:  Unassigned                                                               
Component:  Plugins       |     Version:  2.9                                                                      
 Severity:  normal        |    Keywords:  serialize, postmeta, array, multidimensional, save_post, update_post_meta
--------------------------+-------------------------------------------------
 I've written a small plugin to insert some custom data in the postmeta. I
 have a textarea, in which I write comma-separated values, but I would like
 to store the values in array. So I've tried to save in postmeta a
 multidimensional array, and WP didn't save it. Here is small piece of code
 which should clarify the problem:

 {{{
 <?php
 /********************************************************************
 ADD/EDIT POST VIEW */
 add_action('admin_menu', 'o_add_custom_fields');
 function o_add_custom_fields(){
         add_meta_box( 'myplugin_sectionid', 'Additional Info',
 'o_add_post_fields', 'post', 'advanced' );
 }

 function o_add_post_fields() {
         global $post;
         $data = get_post_meta($post->ID,'_post_data',true);
         if (is_array($data['numbers'])) $data['numbers'] =
 implode(',',$data['numbers']); // convert the array into string, comma
 separated values
 ?>
 <p>
         <label for="numbers">Numbers <small>comma-
 separated</small></label><br />
         <textarea rows="4" cols="25" name="numbers"
 id="numbers"><?=$data['numbers']?></textarea>
 </p>
 <?php

 }

 /********************************************************************
 HANDLE POST REQUEST */
 add_action('save_post', 'o_save_post');
 function o_save_post($post_id) {

         // update meta
         $_POST['numbers'] =
 array_map('trim',explode(',',$_POST['numbers'])); // prepare
         $_post_data = array(
                 'numbers' => $_POST['numbers']
         );

         update_post_meta($post_id, '_post_data', $_post_data);

 }
 ?>
 }}}

 I've noticed this bug in 2.8.6 as well, but there it appears only when you
 insert an image link in the content area using Google Chrome (it adds
 'rel' attribute in the <a> tag and this causes the bug, removing the rel
 does fine.) Weird.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/11579>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list