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 badges1 Answer
Reset to default 0Ok silly me...
I missed this crucial line: add_action( 'save_post', 'save_date_meta_box' );
It now seems to save the date values
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742339049a4425246.html
评论列表(0条)