最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

metabox - Add post meta data date to event

programmeradmin2浏览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/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

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/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

Share Improve this question asked Jun 17, 2020 at 20:37 Zayd BhyatZayd Bhyat 892 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论