Many of the comments below refer to code used for debugging, which for clarity I've removed.
I should have spotted this soon - you are adding only one row into the postmeta table, and that row has the key _my_meta
. Before inserting the array int the table, WordPress serializes the array. When you retrieve the meta, you are retrieving a serialized array which you would then need to unserialize.
However, I don't think this is what you want, (nor is it advisable). It would be better to save each of the values in the array as a separate key-value pair in the database (this would be more effecient, and allowing querying of specific key-values etc).
To do that you need to loop through the received data array and add each key-value individually: (I have relaced your code with appropriate comments)
function my_meta_save($post_id) {
// verify this is not an auto save routine.
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
//Perform nonce checks & permissions (for pages and posts)
//Sanitize the input
//Declare a white list of 'keys' with their defaults (or null)
//- i.e. this code will not add any other keys:
$whitelist = array(
'title'=>null,
'img01-title'=>null,
'img01-url'=>null
);
//It would probably make sense to do this before sanitzing.
$new_data = shortcode_atts($whitelist,$new_data);
//Now add/update or remove key-values appropriately
foreach ($whitelist as $key=>$value){
//If the key has a value, update it. If it doesn't already exist it will add it
if(!is_null($whiltelist[$key]))
update_post_meta($post_id,$key,$value);
else
delete_post_meta($post_id,$key);
}
//No need to return, this is an action not a filter
}
##Remarks##
- I've added an auto-save check so that data is updated when the post/page auto saves
- You've checked permissions if the post is a page - you should do this for posts too.
- I've made use of
shortcode_atts
which will remove any keys that you haven't declared in$whitelist
. It will assign default values to keys that are missing/don't have a value. update_post_meta
will add a new key if it doesn't exist