load_muplugin_textdomain()
Load the translated strings for a plugin residing in the mu-plugins directory.
Uses: load_textdomain()
No Hooks.
Return
true|false
. True when textdomain is successfully loaded, false otherwise.
Usage
load_muplugin_textdomain( $domain, $mu_plugin_rel_path );
- $domain(string) (required)
- Text domain. Unique identifier for retrieving translated strings.
- $mu_plugin_rel_path(string)
- Relative to WPMU_PLUGIN_DIR directory in which the .mo file resides.
Default: empty string
Examples
#1 Register a translation file for the MU plugin
The translation file must be in the plugin directory and must be named like: my-plugin-fr_FR.mo
.
add_action( 'muplugins_loaded', 'myplugin_init' ); function myplugin_init(){ load_muplugin_textdomain( 'my-plugin', dirname( plugin_basename( __FILE__ ) ) ); }
#2 If you want to place the .mo file in the subfolder lang:
We may change the translation file place. For example, let's place it into the lang
subfolder of the plugin. The file must be named like: my-plugin-ru_RU.mo
.
add_action( 'muplugins_loaded', 'myplugin_init' ); function myplugin_init() { load_muplugin_textdomain( 'my-plugin', dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); }
Notes
- Global. WP_Textdomain_Registry. $wp_textdomain_registry WordPress Textdomain Registry.
- Global. Array<String,. WP_Translations|NOOP_Translations> $l10n An array of all currently loaded text domains.
Changelog
Since 3.0.0 | Introduced. |
Since 4.6.0 | The function now tries to load the .mo file from the languages directory first. |
Since 6.7.0 | Translations are no longer immediately loaded, but handed off to the just-in-time loading mechanism. |
load_muplugin_textdomain() load muplugin textdomain code WP 6.8
function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { /** @var WP_Textdomain_Registry $wp_textdomain_registry */ /** @var array<string, WP_Translations|NOOP_Translations> $l10n */ global $wp_textdomain_registry, $l10n; if ( ! is_string( $domain ) ) { return false; } $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ); $wp_textdomain_registry->set_custom_path( $domain, $path ); // If just-in-time loading was triggered before, reset the entry so it can be tried again. if ( isset( $l10n[ $domain ] ) && $l10n[ $domain ] instanceof NOOP_Translations ) { unset( $l10n[ $domain ] ); } return true; }