Using WordPress ‘load_script_textdomain()’ PHP function

The load_script_textdomain() WordPress PHP function loads the translated strings for a specific script.

Usage

load_script_textdomain( $handle, $domain = 'default', $path = '' );

Parameters

  • $handle (string) – Required. Name of the script to register a translation domain to.
  • $domain (string) – Optional. Text domain. Default is ‘default’.
  • $path (string) – Optional. The full file path to the directory containing translation files. Default is an empty string.

More information

See WordPress Developer Resources: load_script_textdomain

Examples

Load translation for a custom script

Load translation for a custom script called ‘my-script’ from the default ‘languages’ directory in the theme.

function my_theme_load_translation() {
    wp_enqueue_script( 'my-script', get_template_directory_uri() . '/js/my-script.js', array(), '1.0.0', true );
    load_script_textdomain( 'my-script', 'my-text-domain', get_template_directory() . '/languages' );
}
add_action( 'wp_enqueue_scripts', 'my_theme_load_translation' );

Load translation for a plugin’s script

Load translation for a plugin’s script called ‘plugin-script’ from a custom ‘translations’ directory inside the plugin folder.

function my_plugin_load_translation() {
    wp_enqueue_script( 'plugin-script', plugin_dir_url( __FILE__ ) . 'js/plugin-script.js', array(), '1.0.0', true );
    load_script_textdomain( 'plugin-script', 'my-plugin-text-domain', plugin_dir_path( __FILE__ ) . 'translations' );
}
add_action( 'wp_enqueue_scripts', 'my_plugin_load_translation' );

Load translation for a script using a custom text domain

Load translation for a script called ‘custom-domain-script’ with a custom text domain ‘custom-domain’.

function load_custom_domain_translation() {
    wp_enqueue_script( 'custom-domain-script', get_template_directory_uri() . '/js/custom-domain-script.js', array(), '1.0.0', true );
    load_script_textdomain( 'custom-domain-script', 'custom-domain', get_template_directory() . '/languages' );
}
add_action( 'wp_enqueue_scripts', 'load_custom_domain_translation' );

Load translation for a script without specifying a path

Load translation for a script called ‘no-path-script’ without specifying a path. This will use the default ‘languages’ directory in the theme.

function load_no_path_translation() {
    wp_enqueue_script( 'no-path-script', get_template_directory_uri() . '/js/no-path-script.js', array(), '1.0.0', true );
    load_script_textdomain( 'no-path-script', 'no-path-domain' );
}
add_action( 'wp_enqueue_scripts', 'load_no_path_translation' );

Load translation for multiple scripts

Load translations for multiple scripts using a single function.

function load_multiple_script_translations() {
    wp_enqueue_script( 'script-1', get_template_directory_uri() . '/js/script-1.js', array(), '1.0.0', true );
    wp_enqueue_script( 'script-2', get_template_directory_uri() . '/js/script-2.js', array(), '1.0.0', true );

    load_script_textdomain( 'script-1', 'domain-1', get_template_directory() . '/languages' );