How do I create a meta box for dates?

I am creating a custom events page and I want to know how to create a custom meta box to save event dates. I don’t want it to be a regular meta box because I will be using this to make a custom search bar so I am guessing I have to save it to MySQL no?

I am lost, as to how I have to go about this. Any ideas?

Read More

Thanks in advance.

Related posts

Leave a Reply

2 comments

  1. WordPress already includes with the scripts to do datepickers.

    If you dont want to do it with a plugin, create a regular text metabox, then you need to enqueue a custom script in the wordpress admin with jquery-ui and jquery-ui-datepicker dependencies:

    function enqueue_date_picker(){
        wp_enqueue_script(
            'field-date', 
            get_template_directory_uri() . '/admin/field-date.js', 
            array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker'),
            time(),
            true
        );  
    
        wp_enqueue_style( 'jquery-ui-datepicker' );
    }
    
    add_action('admin_enqueue_scripts', 'enqueue_date_picker');
    

    Now all we need to do is call the datepicker in our field-date js file:

    jQuery(document).ready(function(){
        jQuery('.datepicker').datepicker(); 
    });
    

    Notice the .datepicker selector? replace this with an id or class for your meta box input’s class/id. Then the datepicker will load once you click on the input – too easy!