WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#11579 closed defect (bug) (worksforme)

WordPress does not serialize properly multidimensional arrays in postmeta

Reported by: ickatanet Owned by: westi
Milestone: Priority: normal
Severity: normal Version: 2.9
Component: Plugins Keywords: serialize, postmeta, array, multidimensional, save_post, update_post_meta
Focuses: 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.

Change History (2)

comment:1 westi4 years ago

  • Milestone Unassigned deleted
  • Resolution set to worksforme
  • Status changed from new to closed

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.

comment:2 ickatanet4 years ago

Looks like array_map doesn't like it when you try to execute it on the same variable. Thanks and sorry for wasting your time :)

Note: See TracTickets for help on using tickets.