wppaste
WordPress

load_script_textdomain( string $handle, string $domain = 'default', string $path = '' ): string|false

Since
5.0.0, 5.0.2, 5.1.0
Source
wp-includes/l10n.php:1145
Loads the script translated strings.

Compatibility

WordPress
since 5.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 register a translation domain to.
$domainstringoptional
Text domain. Default 'default'.Default: 'default'
$pathstringoptional
The full file path to the directory containing translation files.Default: ''

Return value

string|false
The translated strings in JSON encoding on success, false if the script textdomain could not be loaded.

Performance profile

How much work a call to load_script_textdomain() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
92–168

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

Plugin surface
1 hook

Third-party callbacks on 'load_script_textdomain_relative_path' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach query, option, 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 · 16 distinct outcomes

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

WhenInstructionsCalls it makes
isset($handle) && $domain !== "default" && isset($content_url) && isset($plugins_url) && $relative === false92–119wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), apply_filters(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && isset($plugins_url) && isset($site_url) && $relative === false97–130wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), trailingslashit(), apply_filters(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && $relative === false104–131wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), explode(), array_slice(), apply_filters(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && isset($plugins_url) && $relative !== false108–139wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), apply_filters(), str_ends_with(), md5(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && isset($plugins_url) && isset($site_url) && $relative !== false113–150wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), trailingslashit(), apply_filters(), str_ends_with(), md5(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && isset($plugins_url) && $relative !== false113–148wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), apply_filters(), str_ends_with(), md5(), load_script_translations(), load_script_translations()
isset($handle) && $domain !== "default" && $relative === false114–134wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), explode(), explode(), array_slice(), array_slice(), apply_filters(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && isset($plugins_url) && isset($site_url) && $relative !== false118–159wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), trailingslashit(), apply_filters(), str_ends_with(), md5(), load_script_translations(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && $relative !== false120–151wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), explode(), array_slice(), apply_filters(), str_ends_with(), md5(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && isset($plugins_url) && $relative !== false122–153wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), apply_filters(), str_ends_with(), md5(), load_script_translations(), load_script_translations(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && $relative !== false125–160wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), explode(), array_slice(), apply_filters(), str_ends_with(), md5(), load_script_translations(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && isset($plugins_url) && isset($site_url) && $relative !== false127–164wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), trailingslashit(), apply_filters(), str_ends_with(), md5(), load_script_translations(), load_script_translations(), load_script_translations()
4 further outcomes, up to 168 instructions
isset($handle) && $domain !== "default" && $relative !== false130–154wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), explode(), explode(), array_slice(), array_slice(), apply_filters(), str_ends_with(), md5(), load_script_translations()
isset($handle) && $domain !== "default" && isset($content_url) && $relative !== false134–165wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), explode(), array_slice(), apply_filters(), str_ends_with(), md5(), load_script_translations(), load_script_translations(), load_script_translations()
isset($handle) && $domain !== "default" && $relative !== false135–163wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), explode(), explode(), array_slice(), array_slice(), apply_filters(), str_ends_with(), md5(), load_script_translations(), load_script_translations()
isset($handle) && $domain !== "default" && $relative !== false144–168wp_scripts(), determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), plugins_url(), wp_parse_url(), site_url(), wp_parse_url(), get_theme_root(), explode(), explode(), array_slice(), array_slice(), apply_filters(), str_ends_with(), md5(), load_script_translations(), load_script_translations(), load_script_translations()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev26692–16831
8.526692–16831
8.426692–1683149 fewer instructions than PHP 8.3
8.3315102–18731
8.2315102–18731
8.1315102–187317 fewer instructions than PHP 7.4
7.4322105–19231

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.

Hooks and filters fired · 1

One hook fires while load_script_textdomain() runs, in this order:

  1. apply_filters( load_script_textdomain_relative_path )filterline 1252 (+107 into the body)

    Filters the relative path of scripts used for finding translation files.

Uses · 13

  • wp_scripts()Initializes $wp_scripts if it has not been set.
  • determine_locale()Determines the current locale desired for the request.
  • untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
  • load_script_translations()Loads the translation data for the given script handle and text domain.
  • str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
  • wp_parse_url()A wrapper for PHP's parse_url() function that handles consistency in the return values across PHP versions.
  • content_url()Retrieves the URL to the content directory.
  • plugins_url()Retrieves a URL within the plugins or mu-plugins directory.
  • site_url()Retrieves the URL for the current site where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
  • get_theme_root()Retrieves path to themes directory.
  • trailingslashit()Appends a trailing slash.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
Show all 13

Used by · 1

Source code

function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {	/** @var WP_Textdomain_Registry $wp_textdomain_registry */	global $wp_textdomain_registry; 	$wp_scripts = wp_scripts(); 	if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {		return false;	} 	$locale = determine_locale(); 	if ( ! $path ) {		$path = $wp_textdomain_registry->get( $domain, $locale );	} 	$path = untrailingslashit( $path ); 	// If a path was given and the handle file exists simply return it.	$file_base       = 'default' === $domain ? $locale : $domain . '-' . $locale;	$handle_filename = $file_base . '-' . $handle . '.json'; 	if ( $path ) {		$translations = load_script_translations( $path . '/' . $handle_filename, $handle, $domain ); 		if ( $translations ) {			return $translations;		}	} 	$src = $wp_scripts->registered[ $handle ]->src; 	if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && str_starts_with( $src, $wp_scripts->content_url ) ) ) {		$src = $wp_scripts->base_url . $src;	} 	$relative       = false;	$languages_path = WP_LANG_DIR; 	$src_url     = wp_parse_url( $src );	$content_url = wp_parse_url( content_url() );	$plugins_url = wp_parse_url( plugins_url() );	$site_url    = wp_parse_url( site_url() );	$theme_root  = get_theme_root(); 	// If the host is the same or it's a relative URL.	if (		( ! isset( $content_url['path'] ) || str_starts_with( $src_url['path'], $content_url['path'] ) ) &&		( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )	) {		// Make the src relative the specific plugin or theme.		if ( isset( $content_url['path'] ) ) {			$relative = substr( $src_url['path'], strlen( $content_url['path'] ) );		} else {			$relative = $src_url['path'];		}		$relative = trim( $relative, '/' );		$relative = explode( '/', $relative ); 		/*		 * Ensure correct languages path when using a custom `WP_PLUGIN_DIR` / `WP_PLUGIN_URL` configuration,		 * a custom theme root, and/or using Multisite with subdirectories.		 * See https://core.trac.wordpress.org/ticket/60891 and https://core.trac.wordpress.org/ticket/62016.		 */ 		$theme_dir = array_slice( explode( '/', $theme_root ), -1 );		$dirname   = $theme_dir[0] === $relative[0] ? 'themes' : 'plugins'; 		$languages_path = WP_LANG_DIR . '/' . $dirname; 		$relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>.		$relative = implode( '/', $relative );	} elseif (		( ! isset( $plugins_url['path'] ) || str_starts_with( $src_url['path'], $plugins_url['path'] ) ) &&		( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )	) {		// Make the src relative the specific plugin.		if ( isset( $plugins_url['path'] ) ) {			$relative = substr( $src_url['path'], strlen( $plugins_url['path'] ) );		} else {

Changelog

Introduced in 5.0.0. Unchanged from 6.7.7 through 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.

5.1.0
The $domain parameter was made optional.from the docblock
5.0.2
Uses load_script_translations() to load translation data.from the docblock
5.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/l10n.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.