wppaste
WordPress

_load_script_textdomain_from_src( string $handle, string $src, string $domain, string $path, bool $is_module ): string|false

Since
7.0.0
Source
wp-includes/l10n.php:1208
Resolves and loads the translation JSON file for a given script or script module source URL.

Description

This is a shared implementation used by load_script_textdomain() and load_script_module_textdomain() to avoid duplicating the path resolution and file lookup logic.

Compatibility

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

Present in 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$handlestring
Name of the script or script module identifier to register a translation domain to.
$srcstring
Absolute source URL of the script or script module.
$domainstring
Text domain.
$pathstring
The full file path to the directory containing translation files, or an empty string to use the default path from the text domain registry.
$is_modulebool
Whether the source belongs to a script module (true) or a classic script (false).

Return value

string|false
The JSON-encoded translated strings on success, false otherwise.

Performance profile

How much work a call to _load_script_textdomain_from_src() 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
36–165

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

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
2

2 places 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 · 34 distinct outcomes

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

WhenInstructionsCalls it makes
$domain !== "default"36determine_locale(), untrailingslashit(), wp_parse_url(), load_script_translations()
$domain !== "default"46–48determine_locale(), untrailingslashit(), wp_parse_url(), content_url(), wp_parse_url(), load_script_translations()
$domain !== "default" && isset($content_url) && isset($plugins_url) && !is_string($relative)89–113determine_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()
$domain !== "default" && isset($content_url) && isset($plugins_url) && isset($site_url) && !is_string($relative)94–124determine_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()
$domain !== "default" && isset($content_url) && !is_string($relative)101–125determine_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()
$domain !== "default" && !$src_url && isset($src_url) && isset($content_url) && isset($plugins_url) && !is_string($relative)102–116determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && isset($content_url) && isset($plugins_url) && is_string($relative)105–133determine_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()
$domain !== "default" && !$src_url && isset($src_url) && isset($content_url) && isset($plugins_url) && isset($site_url) && !is_string($relative)107–127determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && isset($content_url) && isset($plugins_url) && isset($site_url) && is_string($relative)110–144determine_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()
$domain !== "default" && isset($content_url) && isset($plugins_url) && is_string($relative)110–142determine_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()
$domain !== "default" && !$src_url && isset($content_url) && !is_string($relative)110–128determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && !is_string($relative)111–128determine_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()
22 further outcomes, up to 165 instructions
$domain !== "default" && isset($content_url) && isset($plugins_url) && isset($site_url) && is_string($relative)115–153determine_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()
$domain !== "default" && isset($content_url) && is_string($relative)117–145determine_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()
$domain !== "default" && !$src_url && isset($src_url) && isset($content_url) && isset($plugins_url) && is_string($relative)118–136determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && isset($content_url) && isset($plugins_url) && is_string($relative)119–147determine_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()
$domain !== "default" && !$src_url && !isset($content_url) && !is_string($relative)120–131determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && isset($content_url) && is_string($relative)122–154determine_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()
$domain !== "default" && !$src_url && isset($src_url) && isset($content_url) && isset($plugins_url) && isset($site_url) && is_string($relative)123–147determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && !$src_url && isset($src_url) && isset($content_url) && isset($plugins_url) && is_string($relative)123–145determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && isset($content_url) && isset($plugins_url) && isset($site_url) && is_string($relative)124–158determine_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()
$domain !== "default" && !$src_url && isset($content_url) && is_string($relative)126–148determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && is_string($relative)127–148determine_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()
$domain !== "default" && !$src_url && isset($src_url) && isset($content_url) && isset($plugins_url) && isset($site_url) && is_string($relative)128–156determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && isset($content_url) && is_string($relative)131–159determine_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()
$domain !== "default" && !$src_url && isset($content_url) && is_string($relative)131–157determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && !$src_url && isset($src_url) && isset($content_url) && isset($plugins_url) && is_string($relative)132–150determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && is_string($relative)132–157determine_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()
$domain !== "default" && !$src_url && !isset($content_url) && is_string($relative)136–151determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && !$src_url && isset($src_url) && isset($content_url) && isset($plugins_url) && isset($site_url) && is_string($relative)137–161determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && !$src_url && isset($content_url) && is_string($relative)140–162determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && is_string($relative)141–162determine_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()
$domain !== "default" && !$src_url && !isset($content_url) && is_string($relative)141–160determine_locale(), untrailingslashit(), load_script_translations(), 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()
$domain !== "default" && !$src_url && !isset($content_url) && is_string($relative)150–165determine_locale(), untrailingslashit(), load_script_translations(), 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-dev26936–16531
8.526936–16531
8.426936–1653143 fewer instructions than PHP 8.3
8.331236–17831
8.231236–17831
8.131236–178317 fewer instructions than PHP 7.4
7.431937–18431

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_from_src() runs, in this order:

  1. apply_filters( load_script_textdomain_relative_path )filterline 1316 (+108 into the body)

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

Uses · 12

  • 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.
  • 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.
  • str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
  • trailingslashit()Appends a trailing slash.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.

Used by · 2

Source code

function _load_script_textdomain_from_src( string $handle, string $src, string $domain, string $path, bool $is_module ) {	/** @var WP_Textdomain_Registry $wp_textdomain_registry */	global $wp_textdomain_registry; 	$locale = determine_locale(); 	if ( ! $path ) {		$path = $wp_textdomain_registry->get( $domain, $locale );	} 	if ( $path ) {		$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;		}	} 	$relative       = false;	$languages_path = WP_LANG_DIR; 	$src_url = wp_parse_url( $src );	if ( ! $src_url ) {		return load_script_translations( false, $handle, $domain );	}	$src_url['path'] ??= ''; 	$content_url = wp_parse_url( content_url() );	if ( ! $content_url ) {		return load_script_translations( false, $handle, $domain );	} 	$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 {			$relative = $src_url['path'];

Changelog

Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

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

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.