自定义meta_box的问题

时间:2013-08-02 作者:Luke Wolfenden

我在这里得到了一些信息,我正在开发一个活动网站。

How to add option box in "Edit Post" plugin API?

然而,我已经更改了文本输入的复选框,并且在保存帖子时,这些值会消失。

问题1:如何获取每个帖子要保存的值?问题2:如何将此数据传递到贴子页面?

这是我正在使用的函数,这是问题的图像。

http://i.stack.imgur.com/Epg90.png

// POST PAGE EVENTS BOX

// register the meta box
add_action( \'add_meta_boxes\', \'event_details\' );
function event_details() {
    add_meta_box(
        \'event_details\',
        \'Event Details\',
        \'event_details_content\',
        \'post\',
        \'normal\',
        \'high\'
    );
}

// display the metabox
function event_details_content( $post_id ) {
    // nonce field for security check, you can have the same
    // nonce field for all your meta boxes of same plugin
    wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_nonce\' );

    echo \'<p class="misc-pub-section">Fill in the following details for the event listings page.</p>\';

    echo \'<p class="misc-pub-section"><input type="text" style="padding:10px;" name="event-date" value="\';
    echo date(\'d-m-y\');
    echo \'" /> <b>Date</b> - What date does the event start? <i>Please type in dd-mm-yy format with dashes included</i></p>\';
    echo \'<p class="misc-pub-section"><input type="text" style="padding:10px;" name="event-time" value="\';
    echo date(\'H:i\');
    echo \'"/> <b>Time</b> - What time is the event starting? <i>Please type in 24 hour format with separator colon included</i></p>\';
    echo \'<p class="misc-pub-section"><input type="text" style="padding:10px;" name="event-subtitle"/> <b>Subtitle</b> - Is there an additional subtitle for the event?</p>\' ;
    echo \'<p class="misc-pub-section"><input type="text" style="padding:10px;" name="event-guests"/> <b>Special Guests</b> - Is there any additional guests at the event?</p>\' ;
    echo \'<p class="misc-pub-section"><input type="text" style="padding:10px;" name="event-venue"/> <b>Venue</b> - What venue is the event at?</p>\';    
    echo \'<p class="misc-pub-section"><input type="text" style="padding:10px;" name="event-city"/> <b>City</b> - What city or town is the event in?</p>\';
    echo \'<p class="misc-pub-section"><input type="text" style="padding:10px;" name="event-link"/> <b>Purchase Link</b> - Type the URL link for TicketABC</p>\';
}

// save data from checkboxes
add_action( \'save_post\', \'event_details_data\' );
function event_details_data() {

    // check if this isn\'t an auto save
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
        return;

    // security check
    if ( !wp_verify_nonce( $_POST[\'mypluing_nonce\'], plugin_basename( __FILE__ ) ) )
        return;

    // further checks if you like, 
    // for example particular user, role or maybe post type in case of custom post types

    // now store data in custom fields based on checkboxes selected
    if ( isset( $_POST[\'event-date\'] ) )
        update_post_meta( $post_id, \'event-date\', 1 );
    else
        update_post_meta( $post_id, \'event-date\', 0 );

    if ( isset( $_POST[\'event-time\'] ) )
        update_post_meta( $post_id, \'event-time\', 1 );
    else
        update_post_meta( $post_id, \'event-time\', 0 );

    if ( isset( $_POST[\'event-subtitle\'] ) )
        update_post_meta( $post_id, \'event-subtitle\', 1 );
    else
        update_post_meta( $post_id, \'event-subtitle\', 0 );

    if ( isset( $_POST[\'event-guests\'] ) )
        update_post_meta( $post_id, \'event-guests\', 1 );
    else
        update_post_meta( $post_id, \'event-guests\', 0 );

    if ( isset( $_POST[\'event-venue\'] ) )
        update_post_meta( $post_id, \'event-venue\', 1 );
    else
        update_post_meta( $post_id, \'event-venue\', 0 );

    if ( isset( $_POST[\'event-city\'] ) )
        update_post_meta( $post_id, \'event-city\', 1 );
    else
        update_post_meta( $post_id, \'event-city\', 0 );

    if ( isset( $_POST[\'event-link\'] ) )
        update_post_meta( $post_id, \'event-link\', 1 );
    else
        update_post_meta( $post_id, \'event-link\', 0 );
}


;?>

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

input 领域

$value = get_post_meta($post_id->ID,\'event-link\',true);
<input type="text" style="padding:10px;" value="\'.$value.\'" name="event-link"/>
请注意,元框回调获取的是post对象,而不仅仅是ID。

此外,您根本没有保存字段。相反,您正在保存10. 如果要保存字段,不能只保存伪布尔值。

的第三个参数update_post_meta 是要保存的值。您需要:

if ( isset( $_POST[\'event-date\'] ) )
    update_post_meta( $post_id, \'event-date\', $_POST[\'event-date\'] );
else
    update_post_meta( $post_id, \'event-date\', 0 );

Please look into data sanitization though.

第三,你没有通过临时支票。

wp_nonce_field 如下所示:

wp_nonce_field( $action, $name, $referer, $echo );
wp_verify_nonce 如下所示:

wp_verify_nonce( $nonce, $action );
您需要的是:

wp_nonce_field( \'myplugin_nonce_action\', \'myplugin_nonce\' );
以及:

// security check
if ( !wp_verify_nonce(  $_POST[\'myplugin_nonce\'], \'myplugin_nonce_action\' ) )
    return;
最后,你需要通过$post_id 进入您的save_post 回调。

function event_details_data($post_id) {
现在它工作了。

结束

相关推荐

如何根据$wp_Query中的metabox值更改标记

我有一个称为“问题”的自定义帖子类型,每个问题都有一个内容页。我用发行号标记我的所有文章,因此,例如,所有发行15的文章都被标记\'issue-15\'.我有一个内容的自定义帖子模板,我使用以下代码调用同一问题编号内的所有问题帖子。$wp_query = new WP_Query( array( \'post_type\' => \'issue\', \'tag\' => \'issue-14\', \'posts_per_page\' =&g