Be careful when passing a custom $path to this function (or to WP_Script_Modules::set_translations(), which calls it internally). The official naming convention for script module IDs uses a namespaced format with a slash, e.g. '@my-plugin/my-module' (see the 6.7 Script Modules dev note on make.wordpress.org).
When $path is provided, the translation filename is built as:
$file_base . '-' . $id . '.json'
If your module ID contains a slash — which is the RECOMMENDED format
— this produces a filename like:
my-plugin-pt_BR-@my-plugin/my-module.json
The slash is not escaped, so PHP treats everything before it as a subdirectory. In practice, this means a manually specified $path will silently fail to find your translations unless you create a very unusual nested folder structure matching this exact string, which is not something any translation tool generates.
The safe approach: leave $path empty ('') and let WordPress auto-resolve translations from the module's registered src URL instead. That fallback path (in _load_script_textdomain_from_src()) builds the filename from an MD5 hash of the script's relative path rather than the raw module ID, so it works correctly regardless of slashes in your module ID:
wp_register_script_module( '@my-plugin/my-module', 'build/module.js', array(), '1.0.0' );
add_action( 'init', function () {
wp_script_modules()->set_translations( '@my-plugin/my-module', 'my-plugin' );
// No custom $path — WordPress resolves the translation file
// location from the module's src and locale automatically.
} );
Only pass a custom $path if your module ID has no slashes, or if you've verified the resulting filename actually matches a file you control.
Loads the translation data for a given script module ID and text domain.
DescriptionWorks like load_script_textdomain() but for script modules registered via wp_register_script_module().
Parameters$idstringrequiredThe script module identifier.
$domainstringoptionalText domain. Default 'default'.
Default:'default'
$pathstringoptionalThe full file path to the directory containing translation files.
Default:''
function load_script_module_textdomain( string $id, string $domain = 'default', string $path = '' ) {
$module = wp_script_modules()->get_registered( $id );
if ( null === $module ) {
return false;
}
$src = $module['src'];
// Ensure src is an absolute URL for path resolution.
if ( ! preg_match( '|^(https?:)?//|', $src ) ) {
$src = site_url( $src );
}
return _load_script_textdomain_from_src( $id, $src, $domain, $path, true );
}
View all references View on Trac View on GitHub
Changelog| Version | Description |
|---|---|
| 7.0.0 | Introduced. |
| # | Наименование новости | Тональность | Информативность | Дата публикации |
|---|---|---|---|---|
| 1 | Comment on load_script_textdomain_relative_path by Rodrigo Vieira Eufrasio da Silva | 0 | 8.69 | 14-08-2026 |
| 2 | Comment on WP_Icons_Registry by Rodrigo Vieira Eufrasio da Silva | 0 | 9.38 | 14-08-2026 |
| 3 | Comment on WP_REST_Request::get_param() by Rodrigo Vieira Eufrasio da Silva | 0 | 12.64 | 14-08-2026 |
| 4 | Stop Pasting Translations from ChatGPT | 0 | 10.85 | 24-07-2026 |
| 5 | Comment on resolve_pattern_blocks() by Rodrigo Vieira Eufrasio da Silva | 0 | 10.65 | 14-08-2026 |
| 6 | Comment on wp_deregister_script() by Rodrigo Vieira Eufrasio da Silva | 0 | 29.53 | 14-08-2026 |
| 7 | Bundled symfony/translation-contracts 3.x causes fatal error with other plugins' Symfony copies | 5 | 8 | 27-06-2026 |
| 8 | Неверный путь | -0.5 | 8.52 | 09-08-2026 |
| 9 | AI Translator, plugin para WordPress | 0 | 9.29 | 23-05-2026 |
| 10 | WordPress multiidioma: opciones y trampas | 0 | 8.36 | 04-03-2026 |