wp_localize_script() WP 1.0
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, ... }
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, ... }
Works based on: WP_Scripts::localize()
No Hooks.
Return
true/false. True if the script was successfully localized, false otherwise.
Usage
wp_localize_script( $handle, $object_name, $l10n );
- $handle(string) (required)
- Script handle the data will be attached to.
- $object_name(string) (required)
- Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'.
- $l10n(array) (required)
- The data itself. The data can be either a single or multi-dimensional array.
Notes
- See: WP_Scripts::localize()
- Global. WP_Scripts. $wp_scripts The WP_Scripts object for printing scripts.
Changelog
Since 2.2.0 | Introduced. |
Code of wp_localize_script() wp localize script WP 5.6
function wp_localize_script( $handle, $object_name, $l10n ) {
global $wp_scripts;
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
return false;
}
return $wp_scripts->localize( $handle, $object_name, $l10n );
}