Halloween Sale 15% off Ends in See the plans
PHP

Skip Loading Translations on English Sites

Stops WordPress, plugins and themes from loading translation files on the front end (or everywhere) to save memory and time on sites whose visitors read English anyway, with a list of text domains to keep.

Don’t have WPIDE yet? Install it on your site

Code

// 'frontend' skips translations on public pages only. 'everywhere' also skips them in wp-admin.
$wpide_skip_translations_on = 'frontend';

// Text domains that still load their translations, for example 'default' (WordPress itself) or 'woocommerce'.
$wpide_keep_text_domains = array();

$wpide_skip_translation = function ( $domain ) use ( $wpide_skip_translations_on, $wpide_keep_text_domains ) {
    if ( in_array( $domain, $wpide_keep_text_domains, true ) ) {
        return false;
    }

    if ( 'everywhere' === $wpide_skip_translations_on ) {
        return true;
    }

    if ( is_admin() || wp_doing_ajax() || wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
        return false;
    }

    if ( isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
        return false;
    }

    // REST requests are not flagged yet when the first translations load, so check the address too.
    $uri = isset( $_SERVER['REQUEST_URI'] ) ? (string) wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput

    return ! isset( $_GET['rest_route'] ) && false === strpos( $uri, '/' . rest_get_url_prefix() . '/' ); // phpcs:ignore WordPress.Security.NonceVerification
};

// PHP translation files (.mo and .l10n.php).
add_filter( 'override_load_textdomain', function ( $override, $domain ) use ( $wpide_skip_translation ) {
    return $override || $wpide_skip_translation( $domain );
}, 10, 2 );

// JavaScript translation files (.json).
add_filter( 'pre_load_script_translations', function ( $translations, $file, $handle, $domain ) use ( $wpide_skip_translation ) {
    return ( null === $translations && $wpide_skip_translation( $domain ) ) ? false : $translations;
}, 10, 4 );

Notes

Only makes a difference when the site or user language is not English (United States), for example English (UK), or when plugins ship translations you never show. With 'frontend', wp-admin, the login page, AJAX, REST, cron and WP-CLI keep their translations. Add 'default' to the keep list to keep WordPress's own strings (dates, comment form) translated. Translations a plugin loads before WPIDE runs snippets are not affected.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.