Using WordPress ‘override_load_textdomain’ PHP filter

The override_load_textdomain WordPress PHP filter allows you to override the loading of .mo files for translations. This is useful when you want to handle translations differently or load them from a custom source.

Usage

add_filter('override_load_textdomain', 'your_custom_function', 10, 4);

function your_custom_function($override, $domain, $mofile, $locale) {
    // your custom code here
    return $override;
}

Parameters

  • $override (bool): Whether to override the .mo file loading. Default false.
  • $domain (string): Text domain. Unique identifier for retrieving translated strings.
  • $mofile (string): Path to the MO file.
  • $locale (string|null): Locale.

More information

See WordPress Developer Resources: override_load_textdomain

Examples

Override loading of a specific .mo file

In this example, the loading of the .mo file for the “my-plugin” text domain is overridden and a custom loading mechanism is used.

add_filter('override_load_textdomain', 'override_my_plugin_translation', 10, 4);

function override_my_plugin_translation($override, $domain, $mofile, $locale) {
    if ($domain === 'my-plugin') {
        // Implement custom loading for 'my-plugin' translations
        return true; // Override the .mo file loading
    }
    return $override;
}

Load translations from a custom location

In this example, translations for the “my-plugin” text domain are loaded from a custom location.

add_filter('override_load_textdomain', 'load_my_plugin_translation_from_custom_location', 10, 4);

function load_my_plugin_translation_from_custom_location($override, $domain, $mofile, $locale) {
    if ($domain === 'my-plugin') {
        $custom_mofile = "/path/to/your/custom/mo/files/{$domain}-{$locale}.mo";
        load_textdomain($domain, $custom_mofile);
        return true;
    }
    return $override;
}

Override loading of all .mo files

In this example, the loading of all .mo files is overridden and a custom loading mechanism is used.

add_filter('override_load_textdomain', 'override_all_translations', 10, 4);

function override_all_translations($override, $domain, $mofile, $locale) {
    // Implement custom loading for all translations
    return true;
}

Load translations only for a specific locale

In this example, translations are only loaded for the “en_US” locale.

add_filter('override_load_textdomain', 'load_translations_for_specific_locale', 10, 4);

function load_translations_for_specific_locale($override, $domain, $mofile, $locale) {
    if ($locale === 'en_US') {
        return $override;
    }
    return true; // Prevent loading for other locales
}