Problem in custom meta boxes

时间:2014-12-02 作者:Raunak Hajela

我正在尝试创建一个自定义元框“设置你的情绪”,它将显示用户的情绪,但不幸的是,元框没有保存值。请帮忙

enter image description here

这是我的密码

<?php
/**
 * Adds a box to the main column on the Post and Page edit screens.
 */

function nss_mood_add_meta_box() {
    //$id, $title, $callback, $post_type, $context,$priority, $callback_args 
    add_meta_box(\'nss_mood_id\',\'Set your mood\',\'nss_mood_cb\',\'post\');
} 

add_action(\'add_meta_boxes\',\'nss_mood_add_meta_box\');

// Dispalying form and taking input
function nss_mood_cb() {
    wp_nonce_field(\'mood_meta_box\',\'mood_meta_box_nonce\');
    $mood_value = get_post_meta( $post->ID, \'mood_value_key\', true );

    // Creating our form
    echo \'<p>\';
        echo \'<label for="mood_value_key">What is your mood today ? </label></br></br>\';
        echo \'<input type="text" class="widefat" name="mood_value_key" id="mood_value_key" value="\' . esc_attr( $mood_value ) . \'" />\';
    echo \'</p>\';
}

// Checking value of the form and updating
function save_nss_mood_data($post_id) {

    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( ! isset( $_POST[\'mood_meta_box_nonce\'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'mood_meta_box_nonce\'], \'mood_meta_box\' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don\'t want to do anything.
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user\'s permissions.
    if ( isset( $_POST[\'post_type\'] ) ) {

        if ( ! current_user_can( \'edit_post\', $post_id ) ) {
            return;
        }

    }

    /* OK, it\'s safe for us to save the data now. */

    // Make sure that it is set.
    if ( ! isset( $_POST[\'mood_field\'] ) ) {
        return;
    }

    // Sanitize user input.
    $mood_data = sanitize_text_field( $_POST[\'mood_value_key\'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, \'mood_value_key\', $mood_data );
}

add_action(\'save_post\',\'save_nss_mood_data\');
?>

1 个回复
最合适的回答,由SO网友:Mayeenul Islam 整理而成

好了,问题来了,这是保存数据的完整代码。下面对这些问题进行了解释:

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */

function nss_mood_add_meta_box() {
    //$id, $title, $callback, $post_type, $context,$priority, $callback_args 
    add_meta_box(\'nss_mood_id\',\'Set your mood\',\'nss_mood_cb\',\'post\');
} 

add_action(\'add_meta_boxes\',\'nss_mood_add_meta_box\');

// Dispalying form and taking input
function nss_mood_cb() {
    global $post;
    echo \'<input type="hidden" name="mood_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
    $mood_value = get_post_meta( $post->ID, \'mood_value_key\', true );

    // Creating our form
    echo \'<p>\';
        echo \'<label for="mood_value_key">What is your mood today ? </label></br></br>\';
        echo \'<input type="text" class="widefat" name="mood_value_key" id="mood_value_key" value="\' . esc_attr( $mood_value ) . \'" />\';
    echo \'</p>\';
}

// Checking value of the form and updating
function save_nss_mood_data($post_id) {

    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( empty( $_POST[\'mood_meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'mood_meta_box_nonce\'], basename(__FILE__)) ) return;

    // If this is an autosave, our form has not been submitted, so we don\'t want to do anything.
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user\'s permissions.
    if ( isset( $_POST[\'post_type\'] ) ) {

        if ( ! current_user_can( \'edit_post\', $post_id ) ) {
            return;
        }

    }

    /* OK, it\'s safe for us to save the data now. */

    // Make sure that it is set.
    if ( ! isset( $_POST[\'mood_value_key\'] ) ) {
        return;
    }

    // Sanitize user input.
    $mood_data = sanitize_text_field( $_POST[\'mood_value_key\'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, \'mood_value_key\', $mood_data );
}

add_action(\'save_post\',\'save_nss_mood_data\');
引起干扰的问题:

您的功能nss_mood_cb() 需要$post->ID 但是没有$post 那里所以你需要$post 变量,我使用global $post.

  • 英寸save_nss_mood_data() 我用一种更聪明的方法检查了nonceisset( $_POST[\'mood_field\'] ) 事情不对,所以我改成isset( $_POST[\'mood_value_key\'] ) 因为这实际上来自nss_mood_cb() 回调函数
  • 结束

    相关推荐

    更改标准POST格式Metabox

    我在我的主题(WordPress admin)中添加了一个用于视频、图像、图库和聊天的元框,但我无法为标准帖子添加元框。当我为“Standard”添加帖子类型支持时,admin中的标准帖子格式是重复的。当从列表中选择第二个“标准”时,会添加一个元框,但它应该为第一个“标准”添加元框,并且不会显示两次。