Title: WP_Scripts::localize
Published: April 25, 2014
Last modified: February 24, 2026

---

# WP_Scripts::localize( string $handle, string $object_name, array $l10n ): bool

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#wp--skip-link--target)

Localizes a script, only if the script has already been added.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#parameters)󠁿

 `$handle`stringrequired

Name of the script to attach data to.

`$object_name`stringrequired

Name of the variable that will contain the data.

`$l10n`arrayrequired

Array of data to localize.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#return)󠁿

 bool True on success, false on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#source)󠁿

    ```php
    public function localize( $handle, $object_name, $l10n ) {
    	if ( 'jquery' === $handle ) {
    		$handle = 'jquery-core';
    	}

    	if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present.
    		$after = $l10n['l10n_print_after'];
    		unset( $l10n['l10n_print_after'] );
    	}

    	if ( ! is_array( $l10n ) ) {
    		_doing_it_wrong(
    			__METHOD__,
    			sprintf(
    				/* translators: 1: $l10n, 2: wp_add_inline_script() */
    				__( 'The %1$s parameter must be an array. To pass arbitrary data to scripts, use the %2$s function instead.' ),
    				'<code>$l10n</code>',
    				'<code>wp_add_inline_script()</code>'
    			),
    			'5.7.0'
    		);

    		if ( false === $l10n ) {
    			// This should really not be needed, but is necessary for backward compatibility.
    			$l10n = array( $l10n );
    		}
    	}

    	if ( is_string( $l10n ) ) {
    		$l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' );
    	} elseif ( is_array( $l10n ) ) {
    		foreach ( $l10n as $key => $value ) {
    			if ( ! is_scalar( $value ) ) {
    				continue;
    			}

    			$l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
    		}
    	}

    	$script = "var $object_name = " . wp_json_encode( $l10n, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ';';

    	if ( ! empty( $after ) ) {
    		$script .= "\n$after;";
    	}

    	$data = $this->get_data( $handle, 'data' );

    	if ( ! empty( $data ) ) {
    		$script = "$data\n$script";
    	}

    	return $this->add_data( $handle, 'data', $script );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes_class-wp-scripts-php-2/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-scripts.php#L597)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-scripts.php#L597-L650)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Scripts::add_data()](https://developer.wordpress.org/reference/classes/wp_scripts/add_data/)`wp-includes/class-wp-scripts.php` |

This overrides the add_data method from [WP_Dependencies](https://developer.wordpress.org/reference/classes/wp_dependencies/), to support normalizing of $args.

  | 
| [wp_json_encode()](https://developer.wordpress.org/reference/functions/wp_json_encode/)`wp-includes/functions.php` |

Encodes a variable into JSON, with some confidence checks.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [_doing_it_wrong()](https://developer.wordpress.org/reference/functions/_doing_it_wrong/)`wp-includes/functions.php` |

Marks something as being incorrectly called.

  |

[Show 2 more](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#)

| Used by | Description | 
| [wp_localize_script()](https://developer.wordpress.org/reference/functions/wp_localize_script/)`wp-includes/functions.wp-scripts.php` |

Localizes a script.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_scripts/localize/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.1.0](https://developer.wordpress.org/reference/since/2.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_scripts%2Flocalize%2F)
before being able to contribute a note or feedback.