0

I'm trying to create meta box on a post create/edit page. I faced the problem:

1. When I create a post from scratch the data from my meta box is saved as expected
2. When I try to update an existing post with a new meta box data the data is not saved.

I experimented with a couple of action hooks which I got from wordpress docs and none of them seem to work. Please check the code below:

<?
    add_action('save_post','callback');       // works when a post is created from scratch
    add_action('update_post','callback');     // doesn't work
    add_action('pre_post_update','callback'); // doesn't work
    add_action('publish_post','callback');    // doesn't work

    function callback( $post_ID ) {
        echo "THE POST IS SAVED";

        // Code for saving metabox here
        ...
    };

Don't see any reason why this thing is not working as expected. If anyone faced the same issues please help! I'm desperate

Have a nice day!

1 Answer 1

2

The save_post hook is the only one you need, also, your callback function is missing a parameter, check this example:

/**
 * Save post metadata when a post is saved.
 *
 * @param int $post_id The ID of the post.
 */
function save_book_meta( $post_id ) {

    /*
     * In production code, $slug should be set only once in the plugin,
     * preferably as a class property, rather than in each function that needs it.
     */
    $slug = 'book';

    // If this isn't a 'book' post, don't update it.
    if ( $slug != $_POST['post_type'] ) {
        return;
    }

    // - Update the post's metadata.

    if ( isset( $_REQUEST['book_author'] ) ) {
        update_post_meta( $post_id, 'book_author', sanitize_text_field( $_REQUEST['book_author'] ) );
    }

    if ( isset( $_REQUEST['publisher'] ) ) {
        update_post_meta( $post_id, 'publisher', sanitize_text_field( $_REQUEST['publisher'] ) );
    }

    // Checkboxes are present if checked, absent if not.
    if ( isset( $_REQUEST['inprint'] ) ) {
        update_post_meta( $post_id, 'inprint', TRUE );
    } else {
        update_post_meta( $post_id, 'inprint', FALSE );
    }
}
add_action( 'save_post', 'save_book_meta' );

The example can be found in the Codex page for the save_post action hook.

3
  • Hi Nabil! Thank you for your reply. I used the same snippet as you're referring here and as I described it works ONLY and ONLY when the post is created from scratch. When it goes to UPDATE or whatever else the action hook doesn't shoot. Did it work on your side? Maybe I'm missing something important here?
    – LoomyBear
    Commented May 20, 2014 at 10:29
  • The code should run...add some debugging after update_post_meta and test what it returned, it only returns FALSE if it fails. Oh and yes, I used the hook many times without issues. Commented May 20, 2014 at 10:37
  • 1
    +1 for isset and sanitization!
    – Sisir
    Commented May 20, 2014 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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