自定义POST类型保存后元框为空

时间:2013-09-09 作者:Chris

我正在尝试制作一个包含日期选择器的元框,但我最初尝试获取文本框数据的尝试似乎失败了,因为每次发布或保存文本时,文本都会消失

    <?
/*
Plugin Name: Mobilisation
Plugin URI: http://www.seriadesign.com
Description: Plugin servant a gerer la mobilisation
Version: 0.01
Author: Christophe Rudyj
Author URI: http://www.seriadesign.com
License: Seria-CSN
*/
 // Create out post type
    add_action( \'init\', \'csn_mob_register\' );
    function csn_mob_register() {
    $labels = array(
    \'name\' => _x( \'Mobilisations\', \'mob\' ),
    \'singular_name\' => _x( \'Mobilisation\', \'mob\' ),
    \'add_new\' => _x( \'Ajouter\', \'mob\' ),
    \'add_new_item\' => _x( \'Ajouter une Mobilisation\', \'mob\' ),
    \'edit_item\' => _x( \'Editer une Mobilisation\', \'mob\' ),
    \'new_item\' => _x( \'Ajouter une Mobilisation\', \'mob\' ),
    \'view_item\' => _x( \'Voir une Mobilisation\', \'mob\' ),
    \'search_items\' => _x( \'Rechercher une Mobilisations\', \'mob\' ),
    \'not_found\' => _x( \'Aucune mobilisations trouvée\', \'mob\' ),
    \'not_found_in_trash\' => _x( \'Aucune mobilisations trouvée dans la Corbeille\', \'mob\' ),
    \'parent_item_colon\' => _x( \'Parent Mobilisation:\', \'mob\' ),
    \'menu_name\' => _x( \'Mobilisations\', \'mob\' ),
    );
    $args = array(
    \'labels\' => $labels,
    \'hierarchical\' => false,
    \'supports\' => array( \'title\', \'editor\' ),
    \'taxonomies\' => array( \'category\' ),
    \'public\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'menu_position\' => 5,
    \'show_in_nav_menus\' => true,
    \'publicly_queryable\' => true,
    \'exclude_from_search\' => false,
    \'has_archive\' => true,
    \'query_var\' => true,
    \'can_export\' => true,
    \'rewrite\' => true,
    \'capability_type\' => \'post\'
    );
            register_post_type( \'mob\', $args );
    register_taxonomy_for_object_type( \'category\', \'mob\' );
    } 

function important_category( $id, $post ) {
if ( \'mob\' === get_post_type( $id ) ) {
 $category   = get_term_by( \'slug\', \'important\', \'category\' );
 $categories = wp_get_object_terms( $id, \'category\', array( \'fields\' => \'ids\' ) );
 if ( !in_array( $category->term_id, $categories ) ) {
  wp_set_object_terms( $id, ( int ) $category->term_id, \'category\', true );
 }
}
} add_action( \'wp_insert_post\', \'important_category\' );

/* meta box */
add_action( \'add_meta_boxes\', \'mob_date_add\');
function mob_date_add() {

    add_meta_box( \'mob_datebox\',\'Date de Fin\', \'mob_dateshow\', \'mob\',\'side\', \'default\' );
}

function mob_dateshow() {
global $post;  
    $values = get_post_custom( $post->ID );  
    $text = isset( $values[\'mob_datetext\'] ) ? $values[\'mob_datetext\'] : \'\';  
   wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
    ?>  
    <label for="mob_datetext">Date de fin</label>  
    <input type="text" name="mob_datetext" id="mob_datetext"  value="<?php echo $text; ?>" />   
    <?
}

add_action( \'save_post\', \'mob_datebox_save\', 1, 2);  
function mob_datebox_save( $post, $post_id )  
{  
    // Bail if we\'re doing an auto save  
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return; 

    // if our nonce isn\'t there, or we can\'t verify it, bail 
    if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) return; 

    // if our current user can\'t edit this post, bail  
    if( !current_user_can( \'edit_post\', $post_id ) ) return;  
}  


// now we can actually save the data  
    $allowed = array(   
        \'a\' => array( // on allow a tags  
            \'href\' => array() // and those anchors can only have href attribute  
        )  
    );  

    // Make sure your data is set before trying to save it  
    if( isset( $_POST[\'mob_datetext\'] ) )  
        update_post_meta( $post_id, \'mob_datetext\', wp_kses( $_POST[\'mob_datetext\'], $allowed ) );  




?>

1 个回复
SO网友:Sisir

正如米洛所说。您的功能提前结束。试试这个save_post 行动

add_action( \'save_post\', \'mob_datebox_save\', 1, 2);  
function mob_datebox_save( $post, $post_id )  
{  
    // Bail if we\'re doing an auto save  
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return; 

    // if our nonce isn\'t there, or we can\'t verify it, bail 
    if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) return; 

    // if our current user can\'t edit this post, bail  
    if( !current_user_can( \'edit_post\', $post_id ) ) return;  



// now we can actually save the data  
    $allowed = array(   
        \'a\' => array( // on allow a tags  
            \'href\' => array() // and those anchors can only have href attribute  
        )  
    );  

    // Make sure your data is set before trying to save it  
    if( isset( $_POST[\'mob_datetext\'] ) )  
        update_post_meta( $post_id, \'mob_datetext\', wp_kses( $_POST[\'mob_datetext\'], $allowed ) );  

}
也尝试使用<?php 而不是<? 用于PHP打开。

结束

相关推荐

在Metabox中添加上传按钮

我想为图像添加上传图像按钮并检索url。谁能帮帮我,我已经试了好几天了。我正在使用此线程中的代码Create more Meta Boxes as needed. add_action( \'add_meta_boxes\', \'dynamic_add_custom_box\' ); /* Do something with the data entered */ add_action( \'save_post\', \'dynamic_save_postdata\' );&#