在设计WordPress主题的时候,由于PHP及WordPress版本的问题,可能会产生一些Warning、Notice、Deprecated,根据调整修复的,做一些记录。文章目录Notice: Trying to get property comment_parent of non-objectDeprecated: 函数 wp_blacklist_check 自版本 5.5.0 起已弃用!请使用 wp_check_comment_disallowed_list() 替代。Deprecated: Function get_bloginfo was called with an argument that is deprecated since version 2.2.0! siteurl 选项已被函数 bloginfo() 取代,请改用 url 选项。Deprecated: 函数 get_settings 自版本 2.1.0 起已弃用!请使用 get_option() 替代。Deprecated: 函数 get_currentuserinfo 自版本 4.5.0 起已弃用!请使用 wp_get_current_user() 替代。Notice: Undefined variable: pre_HTML inWarning: Undefined variable $ip1num

在设计WordPress主题的时候,由于PHP及WordPress版本的问题,可能会产生一些Warning、Notice、Deprecated,根据调整修复的,做一些记录。

Notice: Trying to get property comment_parent of non-object

原代码为

if($comment->comment_parent > 0) {

改为

if(isset($comment->comment_parent) && $comment->comment_parent > 0) {

上述改法同样解决了PHP8报的:Warning: Attempt to read property comment_parent on null in

Deprecated: 函数 wp_blacklist_check 自版本 5.5.0 起已弃用!请使用 wp_check_comment_disallowed_list() 替代。

原代码为

if( wp_blacklist_check( $comment['comment_author'],$comment['comment_author_email'],$comment['comment_author_url'], $comment['comment_content'], $comment['comment_author_IP'], $comment['comment_agent'] )) {

改为

if( wp_check_comment_disallowed_list( $comment['comment_author'],$comment['comment_author_email'],$comment['comment_author_url'], $comment['comment_content'], $comment['comment_author_IP'], $comment['comment_agent'] )) {Deprecated: Function get_bloginfo was called with an argument that is deprecated since version 2.2.0! siteurl 选项已被函数 bloginfo() 取代,请改用 url 选项。

原来获取博客首页使用的是

?php bloginfo('siteurl'); ?

改为

?php bloginfo('url'); ?

siteurl,home 在WordPress2.2版本就弃用了。推荐直接使用url。具体可以参考bloginfo()的用法。

Deprecated: 函数 get_settings 自版本 2.1.0 起已弃用!请使用 get_option() 替代。

原来获取博客首页使用的是

?php echo get_settings('Home'); ?

问题与上面一样,get_settings()在WordPress2.1版本就弃用了,提示是用 get_option() 替代

?php echo get_option('home'); ?

当然如果只是获取博客首页,同样可以使用 bloginfo() 。

Deprecated: 函数 get_currentuserinfo 自版本 4.5.0 起已弃用!请使用 wp_get_current_user() 替代。

原代码为

global $current_user;get_currentuserinfo();echo get_avatar( $current_user->user_email, 50);

改为

$current_user = wp_get_current_user();echo get_avatar( $current_user->user_email, 50);Notice: Undefined variable: pre_HTML in

这个基本上就是变量未定义提示的,可以在合适位置加上global定义就行。

global $pre_HTML;Warning: Undefined variable $ip1num

一般情况下php是不需要定义变量的,在PHP8下面,对变量定义要求严格了,这个报错就是使用了未经赋值的变量的值,一般解决方法就是给变量初始化。需要在合适位置加上以下代码。

$ip1num = '';