Using WordPress ‘load_script_textdomain_relative_path’ PHP filter

The load_script_textdomain_relative_path WordPress PHP Filter allows you to filter the relative path of scripts, which is used for locating translation files.

Usage

add_filter( 'load_script_textdomain_relative_path', 'your_custom_function', 10, 2 );

function your_custom_function( $relative, $src ) {
    // Your custom code here

    return $relative;
}

Parameters

  • $relative (string|false) – The relative path of the script. False if it could not be determined.
  • $src (string) – The full source URL of the script.

More information

See WordPress Developer Resources: load_script_textdomain_relative_path

Examples

Remove version query string from script URL

Remove the version query string from the script URL to improve caching.

add_filter( 'load_script_textdomain_relative_path', 'remove_version_query_string', 10, 2 );

function remove_version_query_string( $relative, $src ) {
    // Remove the version query string from the script URL
    $src = remove_query_arg( 'ver', $src );

    return $relative;
}

Set a custom translations folder

Change the default translations folder to a custom folder within your theme.

add_filter( 'load_script_textdomain_relative_path', 'custom_translations_folder', 10, 2 );

function custom_translations_folder( $relative, $src ) {
    // Set the custom translations folder
    $relative = '/your-theme/translations/';

    return $relative;
}

Filter relative path for a specific script

Filter the relative path only for a specific script by checking the $src parameter.

add_filter( 'load_script_textdomain_relative_path', 'specific_script_relative_path', 10, 2 );

function specific_script_relative_path( $relative, $src ) {
    // Check if the script is the one we want to filter
    if ( strpos( $src, 'your-script-name.js' ) !== false ) {
        // Set a custom relative path for this specific script
        $relative = '/custom-path/translations/';
    }

    return $relative;
}

Set different translations folders for multiple scripts

Assign different translations folders for different scripts.

add_filter( 'load_script_textdomain_relative_path', 'multiple_scripts_translations_folders', 10, 2 );

function multiple_scripts_translations_folders( $relative, $src ) {
    // Set a custom translations folder for script1
    if ( strpos( $src, 'script1.js' ) !== false ) {
        $relative = '/custom-path1/translations/';
    }

    // Set a custom translations folder for script2
    if ( strpos( $src, 'script2.js' ) !== false ) {
        $relative = '/custom-path2/translations/';
    }

    return $relative;
}

Skip translations for a specific script

Disable translations for a specific script by returning false.

add_filter( 'load_script_textdomain_relative_path', 'skip_translation_for_script', 10, 2 );

function skip_translation_for_script( $relative, $src ) {
    // Check if the script is the one we want to skip translations
    if ( strpos( $src, 'no-translation-script.js' ) !== false ) {
        // Skip translations for this specific script
        return false;
    }

    return $relative;
}