解决WordPress主题的几个错误

     0评论

本文解决【WP_Widget调用的构造函数已废弃】、【Methods with the same name as their class will not be constructors in a future version of PHP; xxx has a deprecated constructor.】、【wp_deregister_script的调用方法不正确】、【caller_get_posts已被废弃】、【不建议使用get_currentuserinfo】等几个错误。

这些错误是通过Query Monitor发现的,有一段时间了,主要跟主题长久未更新,使用了一些被遗弃的写法有关。

WP_Widget的构造函数错误

在d_banner中为WP_Widget调用的构造函数已自版本4.3.0起废弃!请改用_construct()。

wordpress-theme-error-1 WP_Widget的构造函数错误

原因:使用了类似$this->WP_Widgetparent::WP_Widget/parent的构造函数。

解决办法:将其替换为parent::__construct,这里定位到相关目录后,用sed批量替换。

cd D:\topvps\wp-content\themes\TopVPS\widgets\ sed -i "s/\$this->WP_Widget/parent::__construct/g" *.php sed -i "s/parent::WP_Widget/parent::__construct/g" *.php

构造函数与所属类同名错误

Methods with the same name as their class will not be constructors in a future version of PHP; d_banner has a deprecated constructor.

wordpress-theme-error-2 构造函数与所属类同名错误

解决办法:因为一系列wedgets中都有这个情况,所以查看一个源文件发现规律后,grep找出所有需要替换的情形,然后批量写sed进行替换。

qiushan@topvps MSYS /d/topvps/wp-content/themes/TopVPS/widgets # 找出需修改的文件,其中s结尾的函数不是构造函数,是不需要修改的。 $ grep "function d_[0-9a-z]\+\(\)" *.php wid-banner.php:function d_banners() { wid-banner.php: function d_banner() { wid-comment.php:function d_comments() { wid-comment.php: function d_comment() { wid-postlist.php:function d_postlists() { wid-postlist.php: function d_postlist() { wid-readers.php:function d_readers() { wid-readers.php: function d_reader() { wid-slidebanner.php:function d_slidebanners() { wid-slidebanner.php: function d_slidebanner() { wid-social.php:function d_socials() { wid-social.php: function d_social() { wid-subscribe.php:function d_subscribes() { wid-subscribe.php: function d_subscribe() { wid-tags.php:function d_tags() { wid-tags.php: function d_tag() { wid-textbanner.php:function d_textbanners() { wid-textbanner.php: function d_textbanner() { # 用编辑器批量生成sed语句,针对需要修改的地方进行替换。 sed -i "s/function d_banner()/function __construct()/g" wid-banner.php sed -i "s/function d_comment()/function __construct()/g" wid-comment.php sed -i "s/function d_postlist()/function __construct()/g" wid-postlist.php sed -i "s/function d_reader()/function __construct()/g" wid-readers.php sed -i "s/function d_slidebanner()/function __construct()/g" wid-slidebanner.php sed -i "s/function d_social()/function __construct()/g" wid-social.php sed -i "s/function d_subscribe()/function __construct()/g" wid-subscribe.php sed -i "s/function d_tag()/function __construct()/g" wid-tags.php sed -i "s/function d_textbanner()/function __construct()/g" wid-textbanner.php

wp_deregister_script的调用方法不正确

wp_deregister_script的调用方法不正确。脚本和样式应在wp_enqueue_scripts、admin_enqueue_scripts和login_enqueue_scripts钩子之后再加入加载队列(enqueue)或注册(register)。 请查阅调试WordPress来获取更多信息。 (这个消息是在3.3.0版本添加的。)

wordpress-theme-error-3 wp_deregister_script的调用方法不正确

原因:过早地使用了wp_deregister_script。

解决办法:根据Query Monitor的提示,定位到文件所在行,将相关动作的调用置后。

原来是在after_setup_theme阶段调用的。

add_action('after_setup_theme', 'deal_setup'); function deal_setup() { // ... ... wp_deregister_script('l10n'); // ... ... }

现在将其挪到init钩子这里,问题解决了。

function cancel_l10n() { wp_deregister_script('l10n'); } add_action('init', 'cancel_l10n');

caller_get_posts已被废弃

自3.1.0版本起,已不建议给WP_Query传入一个参数!caller_get_posts已被废弃,请改用ignore_sticky_posts。

caller_get_posts已被废弃 以及 不建议使用get_currentuserinfo

原因:主题中使用了废弃的caller_get_posts函数。

解决方法:换用ignore_sticky_posts函数。例如下面是index页面中的相关代码:

$args = array( 'caller_get_posts' => 1, 'paged' => $paged, ); query_posts($args);

替换为:

$args = array( 'ignore_sticky_posts' => 1, 'paged' => $paged, ); query_posts($args);

因为这类情形比较多,所以用sed批量替换:

qiushan@TopVPS MSYS /d/topvps/wp-content/themes/TopVPS $ find . -name "*.php" | xargs grep caller_get_posts ./index.php: 'caller_get_posts' => 1, ./404.php: 'caller_get_posts' => 1, ./modules/related_list.php: 'caller_get_posts' => 1, ./modules/related_list.php: 'caller_get_posts' => 1, ./modules/related_pic.php: 'caller_get_posts' => 1, ./modules/related_pic.php: 'caller_get_posts' => 1, ./modules/sticky.php: 'caller_get_posts' => 1, ./search.php: 'caller_get_posts' => 1, ./widgets/wid-postlist.php: 'caller_get_posts' => 1, qiushan@TopVPS MSYS /d/topvps/wp-content/themes/TopVPS $ find . -name "*.php" | xargs sed -i "s/caller_get_posts/ignore_sticky_posts/g"

不建议使用get_currentuserinfo

自4.5.0版本起,已不建议使用get_currentuserinfo,请换用wp_get_current_user()。

解决办法:grep找出相关文件,用sed批量替换。

# 找出两个相关文件 qiushan@TopVPS MSYS /d/topvps/wp-content/themes/TopVPS $ find . -name "*.php" | xargs grep get_currentuserinfo ./comments.php: get_currentuserinfo(); ./header.php: get_currentuserinfo(); # 执行替换 qiushan@TopVPS MSYS /d/topvps/wp-content/themes/TopVPS $ find . -name "*.php" | xargs sed -i "s/get_currentuserinfo/wp_get_current_user/g"

网络错误

发生了预料之外的错误。WordPress.org或是此服务器的配置可能出了一些问题。如果您持续遇到困难,请试试支持论坛。(WordPress无法建立到WordPress.org的安全连接,请联系您的服务器管理员。)

wordpress-theme-error-4 网络错误

此类错误一般是向WordPress官方报告统计信息或者获取升级信息时网络连通不畅导致的,忽略即可。

参考资料

-- EOF --

本文最后修改于5年前 (2019-05-08)

差评不太行一般挺好非常不错 (No Ratings Yet)
读取中...
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址