0

I created a post metabox that uses the datepicker to pick a date value. I got as far as adding the metabox and getting the datepicker to work but other than that im struggling to get the data saved to the post. This is my code in my functions.php:

//Add date picker css
function admin_styles() {
    if ( 'events' === get_current_screen()->id ) {
        wp_enqueue_style( 'jquery-ui-smoothness', // wrapped for brevity
            '//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css', [], null );
    }
}
add_action('admin_print_styles', 'admin_styles');
//Activate function
function admin_scripts() {
    if ( 'events' === get_current_screen()->id ) {
        wp_enqueue_script( 'admin', get_template_directory_uri() .'/js/admin.js', [ 'jquery-ui-datepicker' ] );
    }
}
add_action( 'admin_enqueue_scripts', 'admin_scripts' );

//Add date meta-boxes 
function post_date_field($post) {
   echo '<input type="text" id="jquery-datepicker" name="entry_post_date" value="' . get_post_meta( $post->ID, 'entry_post_date', true ) . '">';
}
function post_date_meta_box() {
  add_meta_box('entry_post_date', 'Date', 'post_date_field', 'events', 'side', 'high');
}
add_action('add_meta_boxes', 'post_date_meta_box');

//Save Data
function save_date_meta_box($post_id){
    global $post;
    if( $post->post_type == "events"){
        if (isset($_POST)){
            update_post_meta( $post_id, 'entry_post_date', strip_tags( $_POST['entry_post_date'] ) );
        }
    }
}

After publishing or updating the post I dont think the date value is saved

0

Ok silly me... I missed this crucial line: add_action( 'save_post', 'save_date_meta_box' ); It now seems to save the date values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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