Using WordPress ‘load_script_translation_file’ PHP filter

The load_script_translation_file WordPress PHP Filter allows you to modify the file path for loading script translations for a given script handle and text domain.

Usage

add_filter( 'load_script_translation_file', 'your_custom_function', 10, 3 );

function your_custom_function( $file, $handle, $domain ) {
    // your custom code here
    return $file;
}

Parameters

  • $file (string|false): Path to the translation file to load. False if there isn’t one.
  • $handle (string): Name of the script to register a translation domain to.
  • $domain (string): The text domain.

More information

See WordPress Developer Resources: load_script_translation_file

Examples

Change the translation file path

Change the translation file path for a specific script handle and domain:

add_filter( 'load_script_translation_file', 'change_translation_file_path', 10, 3 );

function change_translation_file_path( $file, $handle, $domain ) {
    if ( 'my-script-handle' === $handle && 'my-text-domain' === $domain ) {
        $file = '/path/to/your/translation/file';
    }
    return $file;
}

Disable translations for a specific script

Disable translations for a specific script handle:

add_filter( 'load_script_translation_file', 'disable_script_translations', 10, 3 );

function disable_script_translations( $file, $handle, $domain ) {
    if ( 'my-script-handle' === $handle ) {
        return false;
    }
    return $file;
}

Load a fallback translation file

Load a fallback translation file if the original file is not found:

add_filter( 'load_script_translation_file', 'load_fallback_translation_file', 10, 3 );

function load_fallback_translation_file( $file, $handle, $domain ) {
    if ( false === $file ) {
        $file = '/path/to/your/fallback/translation/file';
    }
    return $file;
}

Change translation file path based on language

Change the translation file path based on the current site language:

add_filter( 'load_script_translation_file', 'change_translation_file_based_on_language', 10, 3 );

function change_translation_file_based_on_language( $file, $handle, $domain ) {
    $language = get_locale();

    if ( 'my-script-handle' === $handle && 'fr_FR' === $language ) {
        $file = '/path/to/your/french/translation/file';
    }
    return $file;
}

Add a custom translation file for a third-party script

Add a custom translation file for a third-party script:

add_filter( 'load_script_translation_file', 'add_custom_translation_file', 10, 3 );

function add_custom_translation_file( $file, $handle, $domain ) {
    if ( 'third-party-script-handle' === $handle && 'third-party-text-domain' === $domain ) {
        $file = '/path/to/your/custom/translation/file';
    }
    return $file;
}