wp_localize_script

Localize a script.

Description

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

Works only if the script has already been added.

Accepts an associative array $l10n and creates a JavaScript object:

key: value, key: value, ...

Parameters (3)

0. $handle (string)
Script handle the data will be attached to.
1. $object_name (string)
Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: /[a-zA-Z0-9_]+/..
2. $l10n (array)
The data itself. The data can be either a single or multi-dimensional array.

Usage

  1. if ( !function_exists( 'wp_localize_script' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.wp-scripts.php'; 
  3.  
  4. // Script handle the data will be attached to. 
  5. $handle = ''; 
  6.  
  7. // Name for the JavaScript object. Passed directly, so it should be qualified JS variable. 
  8. // Example: '/[a-zA-Z0-9_]+/'. 
  9. $object_name = ''; 
  10.  
  11. // The data itself. The data can be either a single or multi-dimensional array. 
  12. $l10n = array(); 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = wp_localize_script($handle, $object_name, $l10n); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.wp-scripts.php  
  1. function wp_localize_script( $handle, $object_name, $l10n ) { 
  2. global $wp_scripts; 
  3. if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { 
  4. return false; 
  5.  
  6. return $wp_scripts->localize( $handle, $object_name, $l10n );