最近升级到了 WordPress 6.7.1,还是使用 2012 MTR 主题(修改自 twentytwelve v3.8),开启 debug 模式发现 debug.log 文件中不停的报 PHP Notice: 函数 _load_textdomain_just_in_time 的调用方法不正确……
完整 PHP Notice 日志如下:
PHP Notice: 函数 _load_textdomain_just_in_time 的调用方法<strong> 不正确</strong>。 <code>2012_mtr</code> 域的翻译加载触发过早。这通常表示插件或主题中的某些代码运行过早。翻译应在 <code>init</code> 操作或之后加载。 请查阅<a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/"> 调试 WordPress</a> 来获取更多信息。(这个消息是在 6.7.0 版本添加的。)in /data/wwwroot/xuv.cc/wp-includes/functions.php on line 6114
检查主题代码,发现 functions.php 文件中有 load_theme_textdomain( 'twentytwelve' ),不过主题实际并没有翻译文件,于是删除掉,如无意外,问题解决,然而仍然一堆 _load_textdomain_just_in_time 不正确提示……
网上搜索,据说 __e()、__()、_x() 等函数可能也会触发 _load_textdomain_just_in_time,于是把主题所有相关函数都修改掉,这下解决了吧?然而依然报 notice……
再找答案,i18n 可能也会触发翻译,尝试把主题唯一一个 number_format_i18n 修改掉,问题依旧,百思不得其解……
放弃折腾,直接在主题 functions.php 文件中写入如下代码,屏蔽掉:
add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
今天有空继续折腾,虽然主题没有翻译文件,但是还是尝试了一下 init 后假装加载一下翻译,可参考下面代码修改:
错误代码:
add_action('plugins_loaded', function()
{
load_plugin_textdomain('your-textdomain', false, '/path/to/your/plugin/languages/');
});
修改为:
add_action('init', function()
{
load_plugin_textdomain('your-textdomain', false, '/path/to/your/plugin/languages/');
});
毫无惊喜,还是继续 notice……
继续调试,参考 wp-6-7-function-_load_textdomain_just_in_time-was-called-incorrectly,将如下代码添加到 mu-plugin 插件:
<?php
add_action(
'doing_it_wrong_run',
static function ( $function_name ) {
if ( '_load_textdomain_just_in_time' === $function_name ) {
debug_print_backtrace();
error_log(wp_debug_backtrace_summary());
}
}
);
这样可以查看到更详细的信息:
require('wp-blog-header.php'), require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), include('/themes/2012_mtr/functions.php'), ox_get_option, optionsframework_option_name, strtolower, WP_Theme->__toString, WP_Theme->display, WP_Theme->translate_header, translate, get_translations_for_domain, _load_textdomain_just_in_time, _doing_it_wrong, do_action('doing_it_wrong_run'), WP_Hook->do_action, WP_Hook->apply_filters, {closure}
看提示信息, header 处会引入翻译,检查了 header 文件没问题,看到 get_translations_for_domain 忽然想起 style.css 中好像有 domain 字段,该不会有关系吧?
检查主题 style.css 文件,有一行 Text Domain: 2012_mtr,而这个 2012_mtr 就出现在前面的 notice 中……
直接把 Text Domain: 2012_mtr 删除,再刷新,刷新,debug.log 不再增大……
真的是万万没想到,函数 _load_textdomain_just_in_time 的调用方法不正确居然是 style.css 引起,白改了一堆 PHP 文件……
WP 6.7; Function _load_textdomain_just_in_time was called incorrectly maybe related to the style.css file...
《函数 _load_textdomain_just_in_time的调用方法不正确怎么解决?》留言数:0