0

Nothing happens when I do the var_dump() in a callback on post save.

Here are the hooks I've tried:

The code is rather simple:

function my_meta_boxes_save( $post_id ) {
    var_dump($_POST); exit;
    if ( ! wp_verify_nonce( $_POST['my_nonce'], plugin_basename(__FILE__) ) ) {
        return;
    }
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
    //some other checks here etc.
    //update_post_meta($post_id, 'meta_name', 'meta_value');
}
add_action( 'save_post', 'my_meta_boxes_save' ); // also tried the other hooks above

I need this to trigger every time the post is saved, including the cases when only the meta data is changed, and not the post content itself (this is where save_post fails).

Am I missing something? Any suggestions?

Update: @Dan it looks like save_post is triggered only if the post content was updated. I need to add about 10 meta boxes which might be edited without modifying the post content. In that case, when saved, save_post won't be triggered, my callback won't be called and the modifications entered in the custom meta boxes will be discarded.

Update: I am saving the meta boxes using the update_post_meta function. It was just removed from the code above so it looks less messy. I'm ok with users hitting the "Update" button, in fact, this is what I'm after. I do not need any ajax here or automatic save etc. I just need to make sure that my my_meta_boxes_save() callback is going to be called every time the users hit 'Update' or 'Publish' regardless of whether the post content itself was updated or not.

Right now I am debugging this, so I don't even need the meta save to happen - the var_dump() should just output the data, this way showing me that the callback was triggered, but it doesn't.

  • I always use the save_post hook. If you're using a custom meta box this can't be saved without hitting the "update" button so I don't understand what you mean when you say "I need this to trigger every time the post is saved, including the cases when only the meta data is changed, and not the post content itself (this is where save_post fails)." – Dan Dec 7 '14 at 14:18
  • 1
    How are you saving the meta box content without calling the save_post hook? AJAX? – Dan Dec 7 '14 at 14:28
  • 1
    If you want to save metaboxes even if post is not changed add a button "save" to your boxes and handle it with ajax. – gmazzap Dec 7 '14 at 14:31
  • 2
    If you want to save the meta boxes when the post form is submitted, I think save_post is correct, and it should always fire. Make sure the WP dubug tools are turned on and error_log() when the save_post hook is called? – Dan Dec 7 '14 at 14:53
  • 2
    If I put the code you posted here in my test environment in functions.php when I hit the button "Update" without changing anything in the post, I got $_POST dumped and request exit. So the code posted here work. Your problem is somewhere else, or maybe the file where this code is placed is not loaded during a post save action. – gmazzap Dec 7 '14 at 14:54

Your Answer

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

Browse other questions tagged or ask your own question.