WordPress Version: 4.1
/**
* Localize a script.
*
* Works only if the script has already been added.
*
* Accepts an associative array $l10n and creates a JavaScript object:
*
* "$object_name" = {
* key: value,
* key: value,
* ...
* }
*
*
* @see WP_Dependencies::localize()
* @link https://core.trac.wordpress.org/ticket/11520
* @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
*
* @since 2.6.0
*
* @todo Documentation cleanup
*
* @param string $handle Script handle the data will be attached to.
* @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
* Example: '/[a-zA-Z0-9_]+/'.
* @param array $l10n The data itself. The data can be either a single or multi-dimensional array.
* @return bool True if the script was successfully localized, false otherwise.
*/
function wp_localize_script($handle, $object_name, $l10n)
{
global $wp_scripts;
if (!is_a($wp_scripts, 'WP_Scripts')) {
if (!did_action('init')) {
_doing_it_wrong(__FUNCTION__, sprintf(__('Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.'), '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>'), '3.3');
}
return false;
}
return $wp_scripts->localize($handle, $object_name, $l10n);
}