NOTE This plugin is not needed if you are running WordPress 6.5 or later, as the functionality is built into WordPress core. If you need to generate translation in the new
.l10n.php
format, use the Performant Translations plugin instead.
This is a WordPress plugin that caches the .mo file as an PHP array, and include the array instead of the .mo file. In theory, nothing is faster in PHP than loading and executing another PHP file.
Either (recommended):
- Download the plugin files and extract
a-faster-load-textdomain.php
andclass-afld-cachehandler.php
to thewp-content/mu-plugins
directory.
Or:
- Search for "A faster load_textdomain" and install with the WordPress plugin installer.
- (Network) Activate the plugin through the 'Plugins' menu in WordPress.
It's also possible to install the plugin via Composer: composer require soderlind/a-faster-load-textdomain
If you have a plugin or theme that loads a textdomain, e.g. load_textdomain( 'textdomain', $path_to_mo_file )
, then this plugin will:
- Look for a PHP version of the .mo file in
WP_CONTENT_DIR . '/cache/a-faster-load-textdomain'
directory. - If the PHP version exists, include the file.
- If the PHP version doesn't exist, load the .mo file, and save the file as an PHP array in
wp-content/cache/a-faster-load-textdomain/
directory.
graph TD
A[Start] --> B{Look for PHP version of .mo file in cache directory}
B -->|Exists| C[Include the PHP file]
B -->|Doesn't Exist| D[Load the .mo file]
D --> E[Save the file as a PHP array in cache directory]
C --> F[End]
E --> F[End]
The localized PHP array can be cached via PHP OPcache. If you have PHP OPcache enabled, then the localized PHP array will be cached in memory, and the PHP file will not be parsed again.
Change the cache path, default is WP_CONTENT_DIR . '/cache/a-faster-load-textdomain'
.
add_filter( 'a_faster_load_textdomain_cache_path', function( $path ) {
return WP_CONTENT_DIR . '/cache/my-cache';
} );
See CHANGELOG.md.
Orignal file: https://github.com/lynt-smitka/WP-nginx-config/blob/master/extras/mu-plugins/lynt-mo-cache.php
This plugin is copyright © 2023 Per Soderlind.
This plugin is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE for more information.