Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to make a meta box to include a date picker but my initial attempt to have the textbox data seems to fail as each time its either posted or saved again the text disapears

    <?
/*
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 ) );  




?>
share|improve this question
1  
your mob_datebox_save function closes before the post meta is saved. –  Milo Sep 9 '13 at 17:20
    
Are you trying to save it in array? –  Ari Susanto Sep 9 '13 at 17:28

1 Answer 1

As @Milo Said. Your function is ended early. Try this for save_post action.

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 ) );  

}

Also try to use <?php instead of <? for PHP opening.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.