在自定义发布类型中保存元数据

时间:2013-02-20 作者:MF1

我不明白为什么这些函数在保存startdate和starttime时不保存价格和位置数据。当我打印($custom)时,我可以看到[events\\u price]=>数组([0]=>)为空。。

 function event_detail_box_content( $post ) {
    $custom = get_post_custom($post->ID);
    $meta_sd = $custom["events_startdate"][0];
    $meta_pr = $custom["events_price"][0];
    $meta_lo = $custom["events_location"][0];

    $meta_st = $meta_sd;

    $time_format = get_option(\'time_format\');

    if ($meta_sd == null) { $meta_sd = time(); $meta_st = 0;}

    $clean_sd = date("D, M d, Y", $meta_sd);
    $clean_st = date($time_format, $meta_st);

    echo \'<input type="hidden" name="events-nonce" id="events-nonce" value="\' . wp_create_nonce( \'events-nonce\' ) . \'" />\';

    ?>
    <div class="tf-meta">
    <ul>
        <li><label>Event Date</label><input name="events_startdate" class="tfdate" value="<?php echo $clean_sd; ?>" /></li>
        <li><label>Event Time</label><input name="events_starttime" value="<?php echo $clean_st; ?>" /><em>Use 24h format (7pm = 19:00)</em></li>
        <li><label>Event Price</label><input name="events_price" value="<?php echo $meta_pr; ?>" /></li>
        <li><label>Event Location</label><input name="events_location" value="<?php echo $meta_lo; ?>" /></li>
    </ul>
    </div>
    <?php
    print_r($custom);
 }

   add_action (\'save_post\', \'save_events\');

   function save_events($post_id){

    if ( !wp_verify_nonce( $_POST[\'events-nonce\'], \'events-nonce\' )) { return; }

    if ( !current_user_can( \'edit_post\')) return;

    if(!isset($_POST["events_startdate"])): return; endif;

    $updatestartd = strtotime ( $_POST["events_startdate"] . $_POST["events_starttime"] );
    update_post_meta($post_id, "events_startdate", $updatestartd ); 

    update_post_meta($post_id, "events_price", $POST["events_price"] ); 
    update_post_meta($post_id, "events_location", $POST["events_location"] ); 
 }

3 个回复
SO网友:Dwayne Charrington

我可以看到您在上面的代码中犯了几个错误,因此我冒昧地为您修复了它,并告诉我它是否解决了您的问题。您的问题有点含糊,但可以看到您的代码应该可以通过以下修复。

我用双正斜杠注释,其中的值被更改、添加或删除。

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

 function event_detail_box() {
     add_meta_box( 
    \'event_detail_box\',
    __( \'Event Detail\', \'myplugin_textdomain\' ),
    \'event_detail_box_content\',
    \'event\',
    \'normal\',
    \'high\'
    );
}

 function event_detail_box_content( $post ) {
    // No need to globalise a post that is an argument on this callback
    $meta_p = get_post_meta($post->ID, "events_price", true);
    $meta_l = get_post_meta($post->ID, "events_location", true);

    echo \'<input type="hidden" name="events-nonce" id="events-nonce" value="\' . wp_create_nonce( \'events-nonce\' ) . \'" />\';

    ?>
    <div class="tf-meta">
    <ul>
        <li><label>Event Price</label><input name="events_price" value="<?php echo $meta_p; ?>" /></li>
        <li><label>Event Location</label><input name="events_location" size="50" value="<?php echo $meta_l; ?>" /></li>
    </ul>
    </div>
    <?php
 }

 add_action (\'save_post\', \'save_events\');

 // The save_post hook provides the post id to the return function
 function save_events( $post_id ){

    if ( !wp_verify_nonce( $_POST[\'events-nonce\'], \'events-nonce\' )) {
        return $post_id;
    }

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

    update_post_meta($post_id, "events_price", $POST[\'events_price\'] ); 
    update_post_meta($post_id, "events_location", $POST[\'events_location\'] ); 
 }

SO网友:vancoder

我会尝试使用自动传递的post ID来保存\\u事件,而不是$post global。因此:

 function save_events( $post_id ){

    if ( !wp_verify_nonce( $_POST[\'events-nonce\'], \'events-nonce\' )) {
        return $post_id;
    }

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

    update_post_meta($post_id, "events_price", $POST[\'events_price\'] ); 
    update_post_meta($post_id, "events_location", $POST[\'events_location\'] ); 
 }
我不确定global$的帖子是否可靠。

如果失败,请检查您的nonce是否正确接收。

SO网友:MF1

代码的问题是价格和位置没有节约。在进行了大量测试之后,我们发现有一个语法错误,并且中缺少wnderscores

    update_post_meta($post_id, "events_price",  $POST["events_price"] );  
    update_post_meta($post_id, "events_location", $POST["events_location"] ); 
将$POST更改为$\\u POST,修复了该问题。

结束

相关推荐

为什么esc_url在SmartMetabox中不起作用

你试过吗http://www.wproots.com/ultimate-guide-to-meta-boxes-in-wordpress/ ? 创建metabox是一个很好的类。但我对验证有问题。我试过了sanitize_callback 参数来清理URL,所以我使用esc_url 但该字段仍然接受所有值。这里有什么问题?这是我的密码add_smart_meta_box( \'themename_slides_url\', array( \'title\' => _