0
votes

I've created a Custom Post Type 'media-page-items' that I'm attempting to add meta boxes to. Currently the meta boxes are showing but not saving to the database. I've tried a few different approaches on it and none of them seem to save to database.

Debug is turned on but no errors are being thrown currently that I can see.

Any help is much appreciated!

//add article link to media page item
add_action( 'admin_menu', 'gruman_article_link_create' );
add_action( 'save_post', 'gruman_article_link_save', 10, 2 );

function gruman_article_link_create() {
    add_meta_box( 'gruman-article-link', 'Article Link', 'gruman_article_link', 'media-page-items', 'advanced', 'high' );
}

function gruman_article_link( $post ) { 
    // retrieve the _gruman_article_title current value
    $current_article_link = get_post_meta( $post->ID, '_gruman_article_link', true );

?>
    <p>
        <label>Article Link</label>
        <br />
        <input name="gruman-article-link" id="article-link" style="width: 97%;"><?php $current_article_link; ?>/>
        <input type="hidden" name="gruman_article_link_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
    </p>
<?php }

function gruman_article_link_save( $post_id ) {
    // verify taxonomies meta box nonce
    if ( !isset( $_POST['gruman_article_link_nonce'] ) || !wp_verify_nonce( $_POST['gruman_article_link_nonce'], basename( __FILE__ ) ) ){
    return $post_id;
    }

    // return if autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
        return $post_id;
    }

    // Check the user's permissions.
    if ( !current_user_can( 'edit_post', $post_id ) ){
        return $post_id;
    }

    // store article title value
    if ( isset( $_REQUEST['gruman-article-link'] ) ) {
        update_post_meta( $post_id, '_gruman_article_link', sanitize_text_field( $_POST['gruman-article-link'] ) );
    }

}
1

1 Answers

1
votes

In wp_create_nonce you are using plugin_basename( __FILE__ ). And when you verifying nonce you use basename( __FILE__ ) as action name. Those values are not the same. First one will return something like my-plugin/my-plugin.php and the second will be my-plugin.php That is why I believe wp_verify_nonce returns False and your data is not saved.