为什么我的自定义Meta Box字段输入不能保存?

时间:2016-07-14 作者:Migs M.

我有一个标记为“web\\u design”的自定义帖子类型,我希望我的“web\\u design”帖子具有以下自定义字段:
1客户端“-客户端名称
2”exlink’-外部链接datefin’-完成日期

我设法在我的“Web设计”后期管理UI中添加了一个自定义元数据库(标题为“工作信息”),并提供了必要的输入。这是完整的代码。

<?php
//3. Adding Custom Meta Box for Custom Fields
//a. add custom meta box
function work_meta_box( $post ){
    //variables
    $id = \'work_info\';
    $title = \'Work Information\';
    $callback = create_work_meta;
    $screen = \'web_design\';
    $context = \'side\';
    $priority = \'high\';
    add_meta_box( $id, $title, $callback, $screen, $context, $priority );
}
add_action( \'add_meta_boxes_web_design\', \'work_meta_box\' );

//b. callback function for \'work_info\' custom meta box (see $callback variable)
function create_work_meta( $post ){
    wp_nonce_field( basename( __FILE__ ), \'work_meta_box_nonce\' );
    //custom fields
    $client = get_post_meta( $post->ID, \'client\', true );
    $exlink =  get_post_meta( $post->ID, \'exlink\', true );
    $datefin = get_post_meta( $post->ID, \'datefin\', true );

    echo \'<div>
        <p>
            <label for=\\\'client\\\'>Client:</label>
            <br />
            <input type=\\\'text\\\' name=\\\'client\\\' value=\\\'\' . $client .\'\\\' />
        </p>
        <p>
            <label for=\\\'exlink\\\'>External Link:</label>
            <br />
            <input type=\\\'url\\\' name=\\\'exlink\\\' value=\\\'\'. $exlink . \'\\\' />
        </p>
        <p>
            <label for=\\\'datefin\\\'>Date Finished:</label>
            <br />
            <input type=\\\'date\\\' name=\\\'datefin\\\' value=\\\'\'. $datefin . \'\\\' required />
        </p>
    </div>\';
}

//c. saves data after submitting/updating custom post
function save_work_meta( $post_id ){
    //1. verifies meta box nonce (to prevent CSRF attacks)
    if( !isset( $_POST[\'work_meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'work_meta_box_nonce\'] ) ){
        return;
    }
    //2. if autosaves
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ){
        return;
    }
    //3. if user\'s not admin
    if( !current_user_can( \'edit_post\', $post_id ) ){
        return;
    } elseif ( !current_user_can ( \'edit_page\', $post_id) ){
        return;
    }
    //4. checks all custom field values (see \'create_work_meta()\' function)
    if( isset( $_REQUEST[\'client\'] ) ){
        update_post_meta( $post_id, \'client\', sanitize_text_field( $_POST[\'client\'] ) );
    }
    if( isset( $_REQUEST[\'exlink\'] ) ){
        update_post_meta( $post_id, \'exlink\', sanitize_text_field( $_POST[\'exlink\'] ) );
    }
    if( isset( $_REQUEST[\'datefin\'] ) ){
        update_post_meta( $post_id, \'datefin\', sanitize_text_field( $_POST[\'datefin\'] ) );
    }
}
add_action( \'save_post_web_design\', \'save_work_meta\', 10, 2 );
?>
下面是创建“web\\U design”帖子类型的附加代码。我使用的设置可能是这将如何工作的线索。

// H. Creating Custom Posts
// 1. \'Web Design\' Posts (label: \'web_design\')
function web_design_init(){
    //a. array set for \'$labels\' variable
    $labels = array(
        \'name\' => __( \'Web Design\' ),
        \'add_new\' => __( \'Add New Sample\' ),
        \'add_new_item\' => __( \'Web Design Sample\'),
        \'edit_item\' => __( \'Edit Sample\' ),
        \'new_item\' => __( \'New Sample\' ),
        \'search_items\' => __( \'Search Sample\' ),
        \'not_found\' => __( \'No Samples Found\' ),
        \'not_found_in_trash\' => __( \'No Samples to Recover\' )
    );

    //b. array set for \'$supports\' variable
    $supports = array(
        \'title\',
        \'editor\',
        \'thumbnail\',
        \'post-formats\'
    );

    //c. array set for \'$args\' variable
    $args = array(
        \'labels\' => $labels, //a.
        \'description\' => \'For web design works in portfolio.\',
        \'public\' => true,
        \'capability_type\' => \'post\', // <---- I want the \'web_design\' posts to have the same capabilities as a default post item.
        \'menu_position\' => 5,
        \'menu_icon\' => \'dashicons-images-alt\',
        \'hierarchal\' => false,
        \'has_archive\' => true,
        \'supports\' => $supports, //b.
        \'taxonomies\' => array(\'post_tag\')
    );
    //registers \'web_design\' post type
    register_post_type( \'web_design\', $args );
}
add_action( \'init\', \'web_design_init\' );

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

您没有正确验证nonce。试试这个,应该可以:

//1. verifies meta box nonce (to prevent CSRF attacks)
if( !isset( $_POST[\'work_meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'work_meta_box_nonce\'], basename( __FILE__ ) ) ){
        return;
}
有关wp\\u verify\\u nonce的更多信息,请参阅here.

相关推荐

如何在WordPress开发中添加带有ACF自定义字段ID的自定义metabox字段

我是wordpress开发的新手,我在我的项目中安装了高级自定义字段插件,并创建了两个文本字段名称&;我还创建了一个插件,可以在帖子中创建一个带有文本框的元框。现在在帖子中,我将获得自定义字段名称(&A);电子邮件和我的自定义元框旁边将出现,但我必须将我的元框附加到名称字段旁边,即在名称字段和电子邮件字段之间。我的metabox代码如下。请任何人帮帮我//Creating the custom meta box function my_notice_meta_box() {