在自定义帖子类型的metabox中添加exta字段时出现问题

时间:2018-05-10 作者:James Paul

我正在尝试在metabox中添加一个新字段。有一个演示url字段工作正常。我创建了一个新字段,该字段没有存储或保存插入该字段的任何数据。

这是我的大衣。

// Adding meta box for freebie custom post type
function demo_url_meta_box() {

    add_meta_box(
        \'demo_url_meta_box\',
        __( \'Freebie Details\', \'\' ),
        \'demo_url_meta_box_callback\',
        \'freebie\',
        \'normal\',
        \'low\'

    );
}

add_action( \'add_meta_boxes_freebie\', \'demo_url_meta_box\' );
// Callback function of metabox
function demo_url_meta_box_callback( $post ) {

    // Add a nonce field so we can check for it later.
    wp_nonce_field( \'demo_url_nonce\', \'demo_url_nonce\' );

    // postmeta key: _demo_url
    $demo_url = get_post_meta( $post->ID, \'_demo_url\', true );
    ?>
Enter freebie details such as URL of download and also demo URL
<table class="form-table">
    <tbody>
        <tr>
        <th><label for="freebie-demo">Enter demo URL</label></th>
        <td><input type="text" style="width:100%" id="freebie-demo" name="freebie-demo" value="<?php echo $demo_url; ?>"/></td>
        </tr>
        <tr>
        <th><label for="freebie-downurl">Enter Download URL</label></th>
        <td><input type="text" style="width:100%" id="freebie-downurl" name="freebie-downurl" /></td>
        </tr>
    </tbody>
</table>    

<?php
}
/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id
 */
function save_demo_url_meta_box_data( $post_id ) {

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

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'demo_url_nonce\'], \'demo_url_nonce\' ) ) {
        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\'] ) && \'page\' == $_POST[\'post_type\'] ) {

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

    }
    else {

        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[\'freebie-demo\'] ) ) {
        return;
    }

    // Sanitize user input.
    $de_url = sanitize_text_field( $_POST[\'freebie-demo\'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, \'_demo_url\', $de_url );


}
// add meta box data when save_post is hooked
add_action( \'save_post\', \'save_demo_url_meta_box_data\' );

1 个回复
SO网友:Benoti

您没有在save\\u post操作中检索到您的2个值。

你需要获得$\\u POST[\'freebie-demo\'](没问题)和$\\u POST[\'freebie-downurl\']

如果需要基于后置元值的数组,可以执行以下操作:

// Sanitize user input.
$de_url[\'name\'] = sanitize_text_field( $_POST[\'freebie-demo\'] );
$de_url[\'url\'] = esc_url($_POST[\'freebie-downurl\']);

// Update the meta field in the database.
update_post_meta( $post_id, \'_demo_url\', $de_url );
希望它能帮助你!

结束
在自定义帖子类型的metabox中添加exta字段时出现问题 - 小码农CODE - 行之有效找到问题解决它

在自定义帖子类型的metabox中添加exta字段时出现问题

时间:2018-05-10 作者:James Paul

我正在尝试在metabox中添加一个新字段。有一个演示url字段工作正常。我创建了一个新字段,该字段没有存储或保存插入该字段的任何数据。

这是我的大衣。

// Adding meta box for freebie custom post type
function demo_url_meta_box() {

    add_meta_box(
        \'demo_url_meta_box\',
        __( \'Freebie Details\', \'\' ),
        \'demo_url_meta_box_callback\',
        \'freebie\',
        \'normal\',
        \'low\'

    );
}

add_action( \'add_meta_boxes_freebie\', \'demo_url_meta_box\' );
// Callback function of metabox
function demo_url_meta_box_callback( $post ) {

    // Add a nonce field so we can check for it later.
    wp_nonce_field( \'demo_url_nonce\', \'demo_url_nonce\' );

    // postmeta key: _demo_url
    $demo_url = get_post_meta( $post->ID, \'_demo_url\', true );
    ?>
Enter freebie details such as URL of download and also demo URL
<table class="form-table">
    <tbody>
        <tr>
        <th><label for="freebie-demo">Enter demo URL</label></th>
        <td><input type="text" style="width:100%" id="freebie-demo" name="freebie-demo" value="<?php echo $demo_url; ?>"/></td>
        </tr>
        <tr>
        <th><label for="freebie-downurl">Enter Download URL</label></th>
        <td><input type="text" style="width:100%" id="freebie-downurl" name="freebie-downurl" /></td>
        </tr>
    </tbody>
</table>    

<?php
}
/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id
 */
function save_demo_url_meta_box_data( $post_id ) {

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

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'demo_url_nonce\'], \'demo_url_nonce\' ) ) {
        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\'] ) && \'page\' == $_POST[\'post_type\'] ) {

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

    }
    else {

        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[\'freebie-demo\'] ) ) {
        return;
    }

    // Sanitize user input.
    $de_url = sanitize_text_field( $_POST[\'freebie-demo\'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, \'_demo_url\', $de_url );


}
// add meta box data when save_post is hooked
add_action( \'save_post\', \'save_demo_url_meta_box_data\' );

1 个回复
SO网友:Benoti

您没有在save\\u post操作中检索到您的2个值。

你需要获得$\\u POST[\'freebie-demo\'](没问题)和$\\u POST[\'freebie-downurl\']

如果需要基于后置元值的数组,可以执行以下操作:

// Sanitize user input.
$de_url[\'name\'] = sanitize_text_field( $_POST[\'freebie-demo\'] );
$de_url[\'url\'] = esc_url($_POST[\'freebie-downurl\']);

// Update the meta field in the database.
update_post_meta( $post_id, \'_demo_url\', $de_url );
希望它能帮助你!

相关推荐

仅为主页显示Metabox

我将尝试使用设置字段将metabox添加到主页,但出现问题,请帮助我。删除时,metabox不会显示在页面编辑器中if statement 它显示在所有页面上。add_action(\'add_meta_boxes\', \'metabox_homepage_videos\'); function metabox_homepage_videos($post) { if (is_front_page()): add_meta_box(\'metabox