WordPress升级PHP7后wp-code-highlight插件兼容性解决方法

原创内容,转载请注明出处:https://www.myzhenai.com.cn/post/3479.html

关键词:WordPress内容空白 升级PHP7后Wordpress内容空白 wp-code-highlight不能兼容PHP7 preg_replace_callback函数替换preg_replace函数

我刚才把自己服务器的PHP版本从5.4升级到了PHP7,但是在升级后却发现一个问题,所有首页和文章页的文章内容全部是空白的,我知道文章内容还在,但发生这样的问题让我很纳闷。

刚开始我以为是博客主题不兼容PHP7,但经过排查发现不是,我打开了Wordpress的调试功能,终于看到出错的报告文件来自于wp-code-highlight插件。

因为wp-code-highlight里使用了preg_replace函数,而这个函数已经被PHP抛弃了,因为安全问题,PHP7已经不支持这个函数了,现在用了preg_replace_callback函数替换代替。

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in //wp-content/plugins/wp-code-highlight/wp-code-highlight.php on line 68

Warning: preg_replace_callback(): Requires argument 2, ''< p r e class="wp-code-highlight prettyprint linenums:1">'.wch_stripslashes('$2').'</ p r e>'', to be a valid callback in /wp-content/plugins/wp-code-highlight/wp-code-highlight.php on line 68

 

解决方法:
1、找到你博客安装路径,将wp-code-highlight.php这个文件下载到本地,用UE等编辑器打开,修改以下内容。查找preg_replace
2、用下边这一行内容替换掉源文件中的内容

function wp_code_highlight_filter($content) {
    if(get_option('wp_code_highlight_line_numbers')=='enable'){
        $line_numbers=' linenums:1';
    }
    else{
        $line_numbers='';
    }
    return preg_replace("/<pre(.*?)>(.*?)<\/pre>/ise",
        "'<pre class=\"wp-code-highlight prettyprint$line_numbers\">'.wch_stripslashes('$2').'</pre>'", $content);
}
function wp_code_highlight_filter($content) {
    if(get_option('wp_code_highlight_line_numbers')=='enable'){
        $line_numbers=' linenums:1';
    }
    else{
        $line_numbers='';
    }
    return preg_replace_callback('/<pre(.*?)>(.*?)<\/pre>/is',
                function ($m) {
                        return  "<pre class=\"wp-code-highlight prettyprint$line_numbers\">".$m[2].'</pre>';
                }, $content);
}

 

参考内容:https://wordpress.org/support/topic/this-plugin-does-not-support-php7/#post-8043051

文章二维码

扫码在手机上继续阅读

文章二维码