Filters the relative path of scripts used for finding translation files.
Parameters
$relativestring|false- The relative path of the script. False if it could not be determined.
$srcstring- The full source URL of the script.
$is_modulebool- Whether the source belongs to a script module (true) or a classic script (false).
Source
$relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src, $is_module );
This filter exists to solve one specific problem: script translations silently breaking when the script’s src is served from a different host than your WordPress install — most commonly a CDN subdomain (e.g. https://cdn.example.com/my-plugin/build/index.js instead of https://example.com/wp-content/plugins/my-plugin/build/index.js).
Looking at
_load_script_textdomain_from_src()(which fires this filter),$relativeis computed by comparing the script’s host/path againstcontent_url(),plugins_url(), andsite_url()in turn. If none of those hosts match — which happens whenever assets are proxied or served from a CDN with its own domain — $relative is left as false, and translations for that script silently fail to load (no error, the script just renders untranslated).This filter runs right before that failure would happen, letting you supply the correct relative path yourself:
A few things worth knowing:
–
$relativemust end up being a string for translations to load at all — returning false (or anything non-string) means “give up”, which is also useful if you want to explicitly disable translation lookup for specific external scripts instead of triggering warnings.– The
$is_moduleparameter (added in WP 7.0) lets you apply different rewriting logic for script modules (registered viawp_register_script_module()) vs classic scripts, since both share this same resolution function.– The final filename is built as
md5($relative) + '.json'— so$relativedoesn’t need to be a real filesystem path, it just needs to be a STABLE, predictable string that you use consistently when naming your .json translation files (viawp i18n make-json).