WordPress at a glance

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,
	...
}
  • See: WP_Dependencies::localize()
  • Global. WP_Scripts. $wp_scripts The WP_Scripts object for printing scripts.
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.

Code of wp_localize_script: wp-includes/functions.wp-scripts.php VER 4.9.8

<?php
function wp_localize_script( $handle, $object_name, $l10n ) {
	global $wp_scripts;
	if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
		return false;
	}

	return $wp_scripts->localize( $handle, $object_name, $l10n );
}

Related Functions

From tag: script

More from category: Scripts and Styles

No comments
    Hello, !     Log In . Register