wp_localize_script( string $handle, string $object_name, array $l10n )

Localize a script.


Description

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 also


Parameters

$handle

(Required) Script handle the data will be attached to.

$object_name

(Required) Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'.

$l10n

(Required) 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.


Source

File: wp-includes/functions.wp-scripts.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 );
}


Changelog

Changelog
Version Description
WP-2.2.0 Introduced.