#11579 closed defect (bug) (worksforme)
WordPress does not serialize properly multidimensional arrays in postmeta
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Plugins | Version: | 2.9 |
| Severity: | normal | Keywords: | serialize, postmeta, array, multidimensional, save_post, update_post_meta |
| Cc: |
Description
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.
I've taken your code and tidied it up a bit so it doesn't cause php warnings/notices when WP_DEBUG is enabled.
The following works fine for me:
/******************************************************************** 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 ($data) { $numbers = implode(',',$data['numbers']); } ?> <p> <label for="numbers">Numbers <small>comma-separated</small></label><br /> <textarea rows="4" cols="25" name="numbers" id="numbers"><?php echo $numbers;?></textarea> </p> <?php } /******************************************************************** HANDLE POST REQUEST */ add_action('save_post', 'o_save_post'); function o_save_post($post_id) { // update meta if (isset($_POST['numbers'])) { $explode = explode(',',$_POST['numbers']); $map = array_map('trim',$explode); // prepare $_post_data = array( 'numbers' => $map ); update_post_meta($post_id, '_post_data', $_post_data); } }Closing as Works for Me.