惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
T
Threatpost
G
Google Developers Blog
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
H
Heimdal Security Blog
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
Hacker News: Ask HN
Hacker News: Ask HN
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Schneier on Security
B
Blog
V2EX - 技术
V2EX - 技术
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
C
Cybersecurity and Infrastructure Security Agency CISA
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Y
Y Combinator Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
N
News | PayPal Newsroom
Attack and Defense Labs
Attack and Defense Labs
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
S
Security @ Cisco Blogs
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
Project Zero
Project Zero
I
Intezer
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
SecWiki News
SecWiki News
Martin Fowler
Martin Fowler

龙笑天下

Dragon 主题:带用户中心和商城系统的博客 CMS 高级 WordPress 主题 V6.2.0 Dragon Lite 主题:WordPress 博客自媒体资讯主题 V1.4.0 一文带您了解怎么优化WP-Cron计划任务,提升WordPress性能,减少服务器负担 WordPress如何更换网页字体?轻松更换主题的字体为任意您喜欢的自定义字体 使用DragonAI给你的博客文章自动生成AI摘要,支持Wordpress和Typecho 使用开源程序RustDesk自建远程桌面控制连接服务,支持Win/Mac/Android/iOS全平台 程响 - 可能 便宜的半年期200天HTTPS单域名/公网IP/通配符SSL证书 新手从零开始学会使用宝塔面板搭建一个属于自己的Wordpress网站 Debian 12修复OpenSSH远程代码执行高危漏洞:CVE-2024-6387,附Debian系统软件源国内镜像源 谷歌浏览器Google Chrome 149.0.7827.103正式稳定版、测试版及开发版本大全 火狐浏览器Mozilla Firefox 151.0.4正式版、ESR长期版及其它版本大全 不显示Wordpress评论列表里超过N天的评论者的网站链接 Linux Shell脚本实时检测Nginx状态 挂掉则立即自动重启 Blossom,一个支持私有部署的云端存储开源日志笔记软件
WordPress 6.7主题和插件语言翻译加载失效问题解决方法:避免过早调用load_theme_textdomain()和load_plugin_textdomain()
龙笑天 · 2024-11-14 · via 龙笑天下

WordPress 6.7 主题和插件语言翻译加载失效问题解决方法:避免过早调用 load_theme_textdomain()和 load_plugin_textdomain()

今天龙笑天将 WordPress 升级到最新的 6.7 版本,结果各种查看是否有啥 BUG 不兼容的地方,结果发现网站的翻译语言包和问答插件的翻件语言包不加载,会全部丢失显示为英文界面。

网上用 Bing 搜索了一下,发现不是个例,不少盆友都遇到了,或者是主题翻译语言不加载或者是插件翻译文件不加载。PS:在这里龙笑天必须要吐槽一下百度了,自从 2023 年下半年开始,百度不知道搞了啥,中文搜索相关性简直差到离谱,基本搜索不出想要的答案,居然连必应搜索都比不过了,不知道百度是不是要放弃自己这个中文搜索老本行业务了...

究其原因,还是我们的Wordpress偷偷搞事情了,在 WordPress 6.7 引入了更加严格的翻译加载机制。如果主题或插件的代码在 WordPress 初始化时过早地尝试加载翻译文件,WordPress 会发出警告。翻译加载应该等到 WordPress 初始化完毕,才能确保所有的主题和插件文件已正确加载。常见的错误之一就是在 init 钩子之前调用 load_theme_textdomain()或 load_plugin_textdomain(),这会导致 PHP 错误提示,该提示示例如下:

[14-Nov-2024 10:36:09 UTC] PHP Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the your-textdomain domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /wp-includes/functions.php on line 6114

1.解决掉此 PHP Notice 的方法

那么,如何避免并解决掉这个错误提示?下面龙笑天就来为大家讲解下~

1.1 对于主题来说

如果您在开发 WordPress 主题,那么就要确保翻译加载要在after_setup_theme这个钩子中进行,从而避免过早加载翻译文件导致 PHP 报错提示。正确的代码示例如下:

/**
 * WordPress 6.7主题和插件语言翻译加载失效问题解决方法 - 龙笑天下
 * https://www.ilxtx.com/fix-the-issue-of-translations-not-loading-in-wordpress.html
 */
function lxtx_theme_setup() {
    load_theme_textdomain( 'dragon-theme', get_template_directory().'/languages' );
}
add_action('after_setup_theme', 'lxtx_theme_setup');

注意,此示例讲的是您主题的翻译语言包.mo文件应该放置在您主题根目录的languages目录里。

1.2 对于插件来说

如果您在开发 WordPress 插件,那么就要确保翻译加载要在init这个钩子中进行,从而避免过早加载翻译文件导致 PHP 报错提示。正确的代码示例如下:

/**
 * WordPress 6.7主题和插件语言翻译加载失效问题解决方法 - 龙笑天下
 * https://www.ilxtx.com/fix-the-issue-of-translations-not-loading-in-wordpress.html
 */
function lxtx_plugin_setup() {
    load_plugin_textdomain( 'dragon-plugin', plugin_dir_path( __FILE__ ).'languages' );
}
add_action('init', 'lxtx_plugin_setup');

注意,此示例讲的是您主题的翻译语言包.mo文件应该放置在您插件根目录的languages目录里。

2.解决翻译语言包不加载的方法

龙笑天已经按照上面方法,对代码进行了相关处理,结果发现翻译语言包文件还是没有被 Wordpress 加载... 这让龙笑天百思不得骑姐,还是用 Bing 国际版搜索了下,果真找到了解决方法。

方法很简单,将原有使用的调用翻译语言包的函数load_theme_textdomain()load_plugin_textdomain()改为load_textdomain()即可。示例如下:

以主题翻译为例,将原主题翻译的调用方法:

load_theme_textdomain( 'dragon-theme', get_template_directory().'/languages' );

改为如下就完事了:

load_textdomain( 'dragon-theme', get_template_directory().'/languages/'.get_locale().'.mo' );

如果是插件翻译的话,看着上面示例类似操作下就行。在这里,非常感谢 @Alexander Bigga 提供的上面这个解决方法,详见参考文章 1。

完美方法

对于使用了龙笑天下Dragon 主题的盆友,升级到主题的最新版后,可以放心大胆的食用最新版 Wordpress 6.7~

3.相关参考

Translations and styling gone after WP 6.7 update | WordPress.org
Internationalization improvements in 6.7 – Make WordPress Core
#62337 (i18n: Translation is not applied in load_theme_textdomain() function) – WordPress Trac
Translations are no longer loading from the theme folder | WordPress.org
WordPress 6.7 翻译加载问题解决教程:避免过早调用 load_plugin_textdomain()-XinTheme

声明:本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

猜你喜欢