Using WordPress ‘is_textdomain_loaded()’ PHP function

The is_textdomain_loaded() WordPress PHP function checks if the translations for a specified text domain are already loaded.

Usage

is_textdomain_loaded( $domain );

Example:

Input:

is_textdomain_loaded( 'my-plugin' );

Output:

true or false

Parameters

  • $domain (string) – The text domain, which is a unique identifier for retrieving translated strings.

More information

See WordPress Developer Resources: is_textdomain_loaded()

Examples

Unloading a text domain if loaded

Check if the text domain is loaded, and if so, unload it.

$plugin = 'my-plugin';

if ( is_textdomain_loaded( $plugin ) ) {
    unload_textdomain( $plugin );
}

Load a specific text domain for a plugin

Check if the text domain is loaded, and if not, load the translation file for the given locale.

$plugin = 'my-plugin';
$locale = 'de_DE';

if ( ! is_textdomain_loaded( $plugin ) ) {
    $mofile = sprintf( '%s-%s.mo', $plugin, $locale );
    load_plugin_textdomain( $plugin, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' . $mofile );
}

Loading a different text domain for a multilingual website

Unload the current user locale and load the translation file for the requested locale.

$plugin = 'my-plugin';
$locale = 'fr_FR';

if ( is_textdomain_loaded( $plugin ) ) {
    unload_textdomain( $plugin );
}

$mofile = sprintf( '%s-%s.mo', $plugin, $locale );
$domain_path = path_join( WP_LANG_DIR, 'plugins' );
load_textdomain( $plugin, path_join( $domain_path, $mofile ) );

Fall back to plugin language folder if the translation is not found in the WP_LANG_DIR

Load the translation from the plugin’s language folder if it’s not found in the WordPress language directory.

$plugin = 'my-plugin';
$locale = 'es_ES';
$mofile = sprintf( '%s-%s.mo', $plugin, $locale );

$domain_path = path_join( WP_LANG_DIR, 'plugins' );
$loaded = load_textdomain( $plugin, path_join( $domain_path, $mofile ) );

if ( ! $loaded ) {
    $domain_path = path_join( WP_PLUGIN_DIR, "{$plugin}/languages" );
    $loaded = load_textdomain( $plugin, path_join( $domain_path, $mofile ) );
}

Conditional output based on text domain loading

Display a message if the text domain is loaded, otherwise display a different message.

$plugin = 'my-plugin';

if ( is_textdomain_loaded( $plugin ) ) {
    echo __( 'Hello, this is a translated message.', 'my-plugin' );
} else {
    echo 'Hello, this is the default message.';
}