How to get WordPress performing 21% better

If you are using a multilingual WordPress instance there is an occasion to improve the performance in a few simple steps. The trick will give your WordPress roughly 21% better boot-up time which is very important for Google Rank as it impacts directly Time To First Byte.

(In the meantime check our plugin to boost WooCommerce sales)

What field at WordPress are we going to improve?

We are going to improve, and further – speed up the time for loading MO files being used for languages other than en_US.

Unfortunately this cannot be done without touching the core functions…

…but we found a nice way to not make a big mess in the core functions and finally implement the performance improvement in a nice manner using a plugin-style way.

Get your hands dirty!

First you need to find a file under the path your-wp-instance/wp-includes/l10n.php

Then find the function called load_textdomain() and replace it with the definition shown below.

What did we do with the function?

We just added the possibility to filter $domain so that it can be loaded from cache or loaded from file like it is originally implemented. We also added an action hook called locale_imported which gives the possibility to hook by caching layer.

You may copy the ready to use code from our GitHub repository.

Now you are able to use our free simple l10n caching plugin which gives your WordPress a quite speed. That’s all. Enjoy your WordPress doing faster, and getting better positions in Google Rank.

function load_textdomain( $domain, $mofile ) {
	global $l10n, $l10n_unloaded;
	
	/**
	 * Filters whether to load the text domain file loading.
	 *
	 * @since 6.0.0
	 *
	 * @param bool   $plugin_override_locale_domain Whether to override the domain text loading. Default false.
	 * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
	 */
	if ( has_filter( 'locale_load_domain' ) ) {
		$plugin_override_locale_domain = apply_filters( 'locale_load_domain', $domain );

		if ( $plugin_override_locale_domain ) {
			$l10n[ $domain ] = $plugin_override_locale_domain;
			return true;
		}
	}	
	
	$l10n_unloaded = (array) $l10n_unloaded;

	/**
	 * Filters whether to override the .mo file loading.
	 *
	 * @since 2.9.0
	 *
	 * @param bool   $override Whether to override the .mo file loading. Default false.
	 * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
	 * @param string $mofile   Path to the MO file.
	 */
	$plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );

	if ( true === (bool) $plugin_override ) {
		unset( $l10n_unloaded[ $domain ] );

		return true;
	}

	/**
	 * Fires before the MO translation file is loaded.
	 *
	 * @since 2.9.0
	 *
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 * @param string $mofile Path to the .mo file.
	 */
	do_action( 'load_textdomain', $domain, $mofile );

	/**
	 * Filters MO file path for loading translations for a specific text domain.
	 *
	 * @since 2.9.0
	 *
	 * @param string $mofile Path to the MO file.
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 */
	$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );

	if ( ! is_readable( $mofile ) ) {
		return false;
	}

	$mo = new MO();
	if ( ! $mo->import_from_file( $mofile ) ) {
		return false;
	}

	if ( isset( $l10n[ $domain ] ) ) {
		$mo->merge_with( $l10n[ $domain ] );
	}

	unset( $l10n_unloaded[ $domain ] );

	$l10n[ $domain ] = &$mo;

	/**
	 * Fires after the MO translation file is processed.
	 *
	 * @since 6.0.0
	 *
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 * @param string $mo MO object instance.
	 */	
	do_action( 'locale_imported', $domain, $mo );
	
	return true;
}

Leave a Comment

Your email address will not be published.