My custom write panels won’t save data. What am I missing?

So far I’ve been reading some tutorials about ways to create Custom Write Panels. It seems that none of them will really work without some bug. I’d like to discuss one of those tuts here: http://www.sitepoint.com/guide-to-wordpress-custom-write-panels/#.T_iokY7olaQ

I copied this code snippets to my theme and also followed the hints in the comments which adivsed me to correct the syntax of line

Read More
if ( !wp_verify_nonce($_POST['my_meta_noncename'], __FILE__) ) return $post_id;

Having done that, my panel works fine exept one thing: It won’t save the fields data. But if I comment out this line, the fields will get saved but if I then want to delete one of the related posts or create a new one, my backend triggers quirk mode and outputs:

Warning: Invalid argument supplied for foreach() in ...

… my next line which looks like this:

foreach($accepted_fields[$post_type_id] as $key){
    $custom_field = $_POST[$key];
    //If no data is entered
    if      (is_null($custom_field)) { delete_post_meta($post_id, $key); }
    elseif  (isset($custom_field) && !is_null($custom_field))
            { update_post_meta($post_id,$key,$custom_field); } 
    else    { add_post_meta($post_id, $key, $custom_field, TRUE); }
}
return $post_id;
{

How can I fix this?
Your help would be very appreciated. Thanks in advance! Best

Frank

Related posts

Leave a Reply