wppaste
WordPress

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

Since
2.1.0
Source
wp-includes/class-wp-scripts.php:559
Localizes a script, only if the script has already been added.

Compatibility

WordPress
since 2.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$handlestring
Name of the script to attach data to.
$object_namestring
Name of the variable that will contain the data.
$l10narray
Array of data to localize.

Return value

bool
True on success, false on failure.

Performance profile

How much work a call to WP_Scripts::localize() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
36–70

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 87.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()one call below WP_Scripts::localize()

Further down the call graph this can also reach option, query, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Scripts::localize() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_array($l10n) && !is_string($l10n)36–52wp_json_encode(), ->get_data(), ->add_data()
is_array($l10n) && is_string($l10n)41–54html_entity_decode(), wp_json_encode(), ->get_data(), ->add_data()
!is_array($l10n) && !is_string($l10n)51–68__(), sprintf(), _doing_it_wrong(), wp_json_encode(), ->get_data(), ->add_data()
!is_array($l10n) && is_string($l10n)56–70__(), sprintf(), _doing_it_wrong(), html_entity_decode(), wp_json_encode(), ->get_data(), ->add_data()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8736–7012
8.58736–7012
8.48736–7012
8.38736–7012
8.28736–7012
8.18736–70122 fewer instructions than PHP 7.4
7.48936–7012

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 5

  • _doing_it_wrong()Marks something as being incorrectly called.
  • __()Retrieves the translation of $text.
  • wp_json_encode()Encodes a variable into JSON, with some confidence checks.
  • WP_Scripts::get_data()
  • WP_Scripts::add_data()This overrides the add_data method from WP_Dependencies, to support normalizing of $args.

Source code

	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 ) . ';'; 		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 );	}

Changelog

Introduced in 2.1.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.0.4
Parameter $l10n retyped from array to untyped.verified against source
2.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/class-wp-scripts.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.