load_muplugin_textdomain()
Connects the .mo translation file for must-use plugins (mu-plugins) from the specified folder.
The MO file must be named: DOMAIN_TRANSLATION-LOCALE.mo. For example my-plugin-ru_RU.mo, where my-plugin is the translation domain and ru_RU is the translation locale. For available locales, see get_locale().
It is recommended to call the function on the init hook, but it can also be called earlier on the muplugins_loaded, plugins_loaded hooks.
Since WP 6.7.0, translations are not loaded immediately but are registered for loading at the appropriate time (if it has not yet occurred).
If the function is called BEFORE the init hook, the translation will still be loaded on the init hook.
For more details read here.
Since version 4.6, the function first attempts to load the .mo file from the WP_LANG_DIR/plugins/ folder, which is usually /wp-content/language/plugins.
-
Use load_theme_textdomain() to load translations for the theme.
- Use load_plugin_textdomain() to load translations for a regular plugin.
No Hooks.
Returns
true|false. Will return false if the .mo file does not exist at the specified path. In other cases, the function will return true.
Usage
load_muplugin_textdomain( $domain, $mu_plugin_rel_path );
- $domain(string) (required)
- Unique identifier for retrieving the translation string.
- $mu_plugin_rel_path(string)
Path to the .mo file directory relative to WPMU_PLUGIN_DIR.
If the path is not specified, it will be the root directory of MU plugins WPMU_PLUGIN_DIR. That is, the path to the file will be: WPMU_PLUGIN_DIR/domain-ru_RU.mo.
Default: ''
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_registryWordPress Textdomain Registry. - Global. Array<String,. WP_Translations|NOOP_Translations>
$l10nAn 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 7.0
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;
}