Issues I noticed in your code
-
I don’t see where/how you’re calling that custom
datepicker()
function and you should use the sameadmin_enqueue_scripts
hook for registering/enqueueing a stylesheet (.css
) file. -
You should only load the CSS and JS files on the pages where the styles and scripts are being used, e.g. in your case, the post editing screen for your
events
post type, where the screen ID isevents
(the CPT slug). -
$post
is not defined in yourpost_date_field()
function — you should’ve usedfunction post_date_field( $post )
.
Making the Datepicker works
-
First, enqueue the CSS and JS files.
Note: I used the Smoothness theme, but you can just choose whichever you like. I also used the jQuery UI Datepicker library that came bundled in WordPress core where the script handle name is
jquery-ui-datepicker
.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_enqueue_scripts', 'admin_styles' ); function admin_scripts() { if ( 'events' === get_current_screen()->id ) { wp_enqueue_script( 'your-script', // wrapped for brevity '/path/to/your-script.js', [ 'jquery-ui-datepicker' ] ); } } add_action( 'admin_enqueue_scripts', 'admin_scripts' );
-
Then, apply the Datepicker widget to your custom field.
Note: I don’t use the custom
datepicker()
function. Instead, I called the Datepicker directly once the document is ready.// This code should be in your-script.js. jQuery( function ( $ ) { $( '#jquery-datepicker' ).datepicker(); } );