wp_localize_jquery_ui_datepicker()

Localizes the jQuery UI datepicker.

Source

function wp_localize_jquery_ui_datepicker() {
	global $wp_locale;

	if ( ! wp_script_is( 'jquery-ui-datepicker', 'enqueued' ) ) {
		return;
	}

	// Convert the PHP date format into jQuery UI's format.
	$datepicker_date_format = str_replace(
		array(
			'd',
			'j',
			'l',
			'z', // Day.
			'F',
			'M',
			'n',
			'm', // Month.
			'Y',
			'y', // Year.
		),
		array(
			'dd',
			'd',
			'DD',
			'o',
			'MM',
			'M',
			'm',
			'mm',
			'yy',
			'y',
		),
		get_option( 'date_format' )
	);

	$datepicker_defaults = wp_json_encode(
		array(
			'closeText'       => __( 'Close' ),
			'currentText'     => __( 'Today' ),
			'monthNames'      => array_values( $wp_locale->month ),
			'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
			'nextText'        => _x( 'Next', 'datepicker: navigate to next month' ),
			'prevText'        => _x( 'Previous', 'datepicker: navigate to previous month' ),
			'dayNames'        => array_values( $wp_locale->weekday ),
			'dayNamesShort'   => array_values( $wp_locale->weekday_abbrev ),
			'dayNamesMin'     => array_values( $wp_locale->weekday_initial ),
			'dateFormat'      => $datepicker_date_format,
			'firstDay'        => absint( get_option( 'start_of_week' ) ),
			'isRTL'           => $wp_locale->is_rtl(),
		),
		JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
	);

	wp_add_inline_script( 'jquery-ui-datepicker', "jQuery(function(jQuery){jQuery.datepicker.setDefaults({$datepicker_defaults});});" );
}

Changelog

VersionDescription
4.6.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    The conversion from the site’s PHP date_format option to jQuery UI’s date format syntax is done with a fixed str_replace() character map (d, j, l, z, F, M, n, m, Y, y). Any PHP date format character outside that list — for example S (ordinal suffix) or N (ISO-8601 day number) — is copied through literally into the resulting dateFormat string. If you escaped a literal character the PHP way (a backslash, e.g. j\S F Y), the backslash is copied through too, since jQuery UI’s datepicker uses a single quote for escaping, not a backslash. In both cases you’ll see stray, meaningless characters rendered in the datepicker’s input rather than a clean date.

    Also note the timing: this function only localizes the datepicker if wp_script_is( 'jquery-ui-datepicker', 'enqueued' ) is already true at the moment it runs, and it’s hooked on wp_enqueue_scripts / admin_enqueue_scripts at priority 1000. If your own wp_enqueue_script( 'jquery-ui-datepicker', ... ) call happens on a later hook (for example inside wp_footer), the check fails and the localization is silently skipped — the datepicker will still render, just with English defaults and no dateFormat override. See #50040 for related discussion, though note recent reproduction attempts on that ticket have not reliably confirmed the failure in the common footer-enqueue scenario — the underlying condition above is what to check for in your own setup.

You must log in before being able to contribute a note or feedback.