7

I have two custom meta fields that I've enabled for each post, scottb_customHeader and scottb_customTitle

These work fine as long as I'm using the full edit feature to edit posts. However, when I click "Quick Edit", then click "Update", my custom meta values for the post are cleared out. What do I need to do to resolve?

Code is below...

add_action('save_post', 'custom_add_save');


function custom_add_save($postID){
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $postID;
    }
    else
    {
        // called after a post or page is saved
        if($parent_id = wp_is_post_revision($postID))
        {
        $postID = $parent_id;
        }

        if ($_POST['scottb_customHeader']) 
        {
            update_custom_meta($postID, $_POST['scottb_customHeader'], '_scottb_customHeader');
        }
        else
        {
            update_custom_meta($postID, '', '_scottb_customHeader');
        }
        if ($_POST['scottb_customTitle']) 
        {
            update_custom_meta($postID, $_POST['scottb_customTitle'], '_scottb_customTitle');
        }
        else
        {
            update_custom_meta($postID, '', '_scottb_customTitle');
        }
    }
}

function update_custom_meta($postID, $newvalue, $field_name) {
    // To create new meta
    if(!get_post_meta($postID, $field_name)){
    add_post_meta($postID, $field_name, $newvalue);
    }else{
    // or to update existing meta
    update_post_meta($postID, $field_name, $newvalue);
    }
}
  • @Scott: If you use the Quick Edit, I assume your meta fields are not displayed? In that case $_POST['scottb_customHeader'] will always be empty and you inadvertently clear them. Your custom_add_save should probably not fire when you do a quick edit? – Jan Fabry Dec 6 '10 at 16:38
  • 3
    Check if DOING_AJAX is defined and true - if it is, return the post ID as you do with DOING_AUTOSAVE currently. – TheDeadMedic Dec 6 '10 at 19:00
  • @TheDeadMedic: I suspect that approach will become broken with the WP 3.1 ajaxified admin. :-D – Denis de Bernardy Dec 6 '10 at 19:02
  • @Jan - correct on all counts :) – Scott B Dec 6 '10 at 20:55
  • @Denis - say wha? Have they ajaxified the main post edit screen too now? – TheDeadMedic Dec 6 '10 at 22:02
4

Add a hidden flag to the post edit form along with your custom fields. Something like

<input type="hidden" name="my_hidden_flag" value="true" />

Then, wrap all of your custom save_post stuff in a check for this flag. Then you don't have to check for the autosave constant any more either--if the flag doesn't exist, it's either a quick edit or an autosave.

function custom_add_save($postID){

    // Only do this if our custom flag is present
    if (isset($_POST['my_hidden_flag'])) {

        // called after a post or page is saved
        if($parent_id = wp_is_post_revision($postID)) {
            $postID = $parent_id;
        }

        if ($_POST['scottb_customHeader']) {
            update_custom_meta($postID, $_POST['scottb_customHeader'], '_scottb_customHeader');
        } else {
            update_custom_meta($postID, '', '_scottb_customHeader');
        }

        if ($_POST['scottb_customTitle']) {
            update_custom_meta($postID, $_POST['scottb_customTitle'], '_scottb_customTitle');
        } else {
            update_custom_meta($postID, '', '_scottb_customTitle');
        }

    }

}
  • Just coming back to this one. Works brilliantly. Thanks MathSmath! – Scott B Jan 13 '11 at 23:39
9

I had the same issue. Just add the following code at the beginning of the save_post action hook callback function (the function used to save the custom data).

// handle the case when the custom post is quick edited
// otherwise all custom meta fields are cleared out
if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
      return;

What it actually does: it checks if the quick saving wp_nonce_field exists and returns if that's the case. No need to create an additional hidden field in the form.

1

The question is quite old, but I think that problem stil exists.

To be honest there is no good answer here because there is nothing about security. You save data to database without checking their source...

You should use nonce to verify the source of data - thanks to this they wont be overwrite (nor delete) by QuickEdit. WordPress give you utils for this

Add to your form:

wp_nonce_field('my_custom_page', '_my_custom_page');

And than at the beggining of your save function:

if (!wp_verify_nonce( $_POST['_my_custom_page'], 'my_custom_page' )) { return $post_id; }

You should do this for each custom post.

0

What happens is: The new action on 'save_post' is not equipped to handle the new data that you have in your custom fields. So when the save post runs, it's missing all the custom field $_POST data, and thus deletes the field values, leaving you with nothing.

FIX:

Where ever the add_action('save_post','your_new_action'); is, you can make it return early if you are on 'edit' screen (where the quick edits happen):

 global $current_screen;    
 if($current_screen->base == 'edit') return $post_id;

Or you can check weather the $_POST action is an inline-save (the quick edit's action name) and then not execute anything after that thus preserving your custom field values:

 if( $_POST['action'] != 'inline-save' ) { echo 'Do this then..'; }

I might have to elaborate even more if you're not sure where to put these snippets. But I hope it helps someone with similar issues.

0

The problem here is that WP sees that your fields are not set, and you are telling WP to clear them out. Don't clear it out if the field doesn't exist. Clear it out if the field exists, but is empty.

    if ( isset( $_POST[$meta_key] ) ){
        $meta_value = $_POST[$meta_key];
        if ($meta_value) {
            update_term_meta($term_id, $meta_key, $meta_value);
        } else {
            delete_term_meta($term_id, $meta_key);
        }
    }
  • 1
    Please, do not use unsanitized and unvalidated and unescaped values when you save something to the DB. This is highly dangerous. – kaiser Jan 30 '17 at 21:37
  • @kaiser According to WP: $meta_value (mixed) (required) The new value of the custom field. A passed array will be serialized into a string.(this should be raw as opposed to sanitized for database queries) codex.wordpress.org/Function_Reference/update_post_meta – Tim Bunch Feb 1 '17 at 15:01
  • Aside from your reference being about post meta while your answer handles term meta, this is nonsense. Always sanitize database input, no matter what. While both use update_metadata() under the hood, the function that cares about sanitizing is sanitize_meta(). it's a filter and could be deregistered. Either you try to make sure to attach your own callback, or you simply do not trust it. – kaiser Feb 1 '17 at 16:13
  • In case, you might want to just var_dump( $GLOBALS['wp_filter']['sanitize_meta'] ); at the appropriate position in code, to see what is attached and what does what. – kaiser Feb 1 '17 at 16:14
  • 1
    Quite right. The correct link for term meta is codex.wordpress.org/Function_Reference/update_term_meta - same process. – Tim Bunch Feb 1 '17 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.