Using WordPress ‘override_unload_textdomain’ PHP filter

The override_unload_textdomain WordPress PHP filter allows you to control if a text domain should be prevented from unloading.

Usage

add_filter('override_unload_textdomain', 'your_custom_function', 10, 3);

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

Parameters

  • $override (bool): Whether to override the text domain unloading. Default false.
  • $domain (string): Text domain. Unique identifier for retrieving translated strings.
  • $reloadable (bool): Whether the text domain can be loaded just-in-time again.

More information

See WordPress Developer Resources: override_unload_textdomain

Examples

Prevent a specific text domain from unloading

To prevent a specific text domain from unloading, check if the $domain matches the desired text domain and set $override to true.

add_filter('override_unload_textdomain', 'prevent_unload_my_textdomain', 10, 3);

function prevent_unload_my_textdomain($override, $domain, $reloadable) {
    if ($domain === 'my-textdomain') {
        $override = true;
    }
    return $override;
}

Only allow reloadable text domains to unload

If you want to only allow reloadable text domains to unload, check the $reloadable value and set $override accordingly.

add_filter('override_unload_textdomain', 'allow_only_reloadable_unload', 10, 3);

function allow_only_reloadable_unload($override, $domain, $reloadable) {
    $override = !$reloadable;
    return $override;
}

Prevent all text domains from unloading

To prevent all text domains from unloading, simply set $override to true for all text domains.

add_filter('override_unload_textdomain', 'prevent_all_unload', 10, 3);

function prevent_all_unload($override, $domain, $reloadable) {
    $override = true;
    return $override;
}

Log text domains before unloading

If you want to log the text domains before unloading them, you can use this filter to achieve that.

add_filter('override_unload_textdomain', 'log_textdomain_unload', 10, 3);

function log_textdomain_unload($override, $domain, $reloadable) {
    error_log('Unloading text domain: ' . $domain);
    return $override;
}

Modify the unload behavior based on a custom condition

You can also modify the unload behavior based on custom conditions, such as user role or capabilities.

add_filter('override_unload_textdomain', 'custom_unload_condition', 10, 3);

function custom_unload_condition($override, $domain, $reloadable) {
    if (current_user_can('manage_options')) {
        $override = true;
    }
    return $override;
}