WordPress加载缓慢求助:load_plugin_textdomain等函数耗时超60%
Hey there, sorry to hear you're stuck with this 3-second load time tied directly to translation functions—let's break down the most likely fixes for your issue:
1. Optimize Your MO File Integrity & Size
Corrupted or bloated .mo files are a common culprit here. Even if your .po looks fine, auto-generated .mo files can sometimes carry redundant data or formatting that slows parsing. Try these steps:
- Recompile your
.poto.mousing the officialmsgfmttool (part of gettext utilities) instead of relying on third-party generators:msgfmt -o cs_CZ.mo cs_CZ.po - Audit your
.pofile for unused entries (like deprecated plugin strings or debug text) and delete them before recompiling—fewer entries mean faster parsing.
2. Add Translation Caching
WordPress doesn't cache translation data by default, so every page load re-parses the entire .mo file. You can add custom caching via your theme's functions.php to store parsed translations in WordPress object cache:
add_filter('override_load_textdomain', function($override, $domain, $mofile) { // Replace 'your-domain' with your theme/plugin's text domain if ($domain === 'your-domain') { $cache_key = 'translations_' . md5($mofile); $cached_translations = wp_cache_get($cache_key); if ($cached_translations) { // Use cached translations instead of reloading the MO file global $l10n; $l10n[$domain] = $cached_translations; return true; } // Load and cache the translations if not already cached $translations = load_textdomain($domain, $mofile); if ($translations) { wp_cache_set($cache_key, $translations, '', 86400); // Cache for 24 hours } } return $override; }, 10, 3);
If you're using a persistent object cache (like Redis or Memcached), this will give even bigger performance gains.
3. Track Down Duplicate Load Calls
Sometimes plugins or themes accidentally call load_plugin_textdomain multiple times for the same text domain, forcing WordPress to re-parse the MO file repeatedly. Use xDebug's call stack to:
- Identify which scripts are triggering these functions
- Check if you can remove redundant calls (e.g., a plugin loading its translation both on
initandplugins_loaded)
4. Verify File System Performance
Even though both local and production are slow, double-check that your .mo file isn't stored on a slow filesystem (like a network share for production, or a fragmented local drive). Try:
- Moving the MO file to a directory with faster read speeds
- Ensuring file permissions are set correctly (no extra overhead from permission checks)
5. Test with Minimal Environment
Disable all non-essential plugins and switch to a default theme (like Twenty Twenty-Four) to see if the load time drops. If it does, re-enable plugins one by one to find if a specific plugin is conflicting with the translation loading process.
Once you've tried these steps, use tools like Query Monitor to re-measure the time spent on translation functions—you should see a significant drop if the issue was tied to one of these causes.
内容的提问来源于stack exchange,提问作者Kryštof Matěj

