Why jQuery Datepickers Fail in WordPress Templates
You've added a datepicker to your form, but it remains invisible on the page. This is a classic conflict between WordPress's script management system and external libraries. The root cause isn't usually the HTML markup or the basic jQuery logic; it is almost always how the scripts are loaded relative to one another.
Your current setup attempts to force Google CDN versions of jQuery and jQuery UI while simultaneously relying on WordPress's default enqueuing hooks. This creates a race condition where the browser loads scripts in an order that breaks your datepicker initialization. Specifically, you are deregistering the local jQuery only if !is_admin(), but then immediately registering a specific version from Google. While this works for pure jQuery, it complicates the loading of jQuery UI components which WordPress expects to find under a specific handle.
The critical issue in your functions.php is the manual enqueueing of jquery-ui-core and jquery-ui-slider without properly handling the dependencies or the theme path. When you manually add wp_enqueue_script('jquery-ui-core'), WordPress looks for this asset relative to your active theme's root directory (/js/ui/jquery-ui-core.js). If that file doesn't exist, it fails silently unless you provide a full URL. Your code attempts to patch this by defining $urlui and enqueuing it with wp_enqueue_style, but there is a fundamental misunderstanding of how jQuery UI assets work. You cannot enqueue a .js file using wp_enqueue_style. That function is exclusively for CSS.
To fix the initialization failure, you must ensure that the specific version of jQuery UI your datepicker expects is actually loaded in the browser. The standard WordPress jQuery UI bundle often includes different versions than the Google CDN you are linking to directly. Mixing local assets with CDNs without careful dependency management leads to the "jQuery is not defined" or "jQuery UI is missing" errors that prevent the datepicker widget from rendering.
Here is a corrected approach that respects WordPress's asset loading lifecycle. Instead of manually mixing CDN links and local enqueues, it is safer to use the wp_enqueue_script function to load jQuery UI components from your theme's /js/ folder or a reliable CDN with the correct dependency flags set.
function enqueue_datepicker_assets() {
// Ensure jQuery is loaded first if not already handled by WordPress core
wp_enqueue_script('jquery');
// Load jQuery UI Core and Datepicker specifically
// We use local paths relative to the theme root, but you can replace
// these with full CDN URLs if your local files are missing.
$js_url = get_template_directory_uri() . '/js/';
wp_enqueue_script( 'jquery-ui-core', $js_url . 'jquery-ui.min.js', array('jquery'), false, true );
// Ensure the datepicker component is loaded explicitly as it often needs a separate handle
wp_enqueue_script( 'jquery-ui-datepicker', $js_url . 'jquery-ui-datepicker.min.js',
array('jquery-ui-core', 'jquery'), false, true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_datepicker_assets' );
In your template file (page-mypage.php), the HTML structure you provided is correct. The script block using $(function() { ... }) is also appropriate because it ensures the DOM is ready before the datepicker is initialized. However, verify that the input ID matches exactly. In your example, the input has id="datepicker", and the jQuery selector targets #datepicker. This alignment is correct.
The failure to display usually stems from the script not executing at all due to missing dependencies. By switching to the wp_enqueue_script method with proper dependency arrays (the second argument in wp_enqueue_script), you guarantee that jQuery loads before jQuery UI, and jQuery UI loads before your custom script runs. This eliminates the timing issues inherent in manually manipulating $wp_scripts.
If you require a specific version of jQuery UI from a CDN rather than local files, ensure you enqueue it as a script, not a style. You would define the full URL for the JS file and append array('jquery') to the dependency list. Remember that mixing multiple versions of jQuery or jQuery UI can introduce subtle bugs where the plugin behaves unexpectedly even if no console errors appear. Stick to one consistent versioning strategy across your project to maintain stability.