博客 / WordPress/ 【修正报错】WordPress百度搜索自动推送、主动收录JS代码

【修正报错】WordPress百度搜索自动推送、主动收录JS代码

【修正报错】WordPress百度搜索自动推送、主动收录JS代码

问题描述

原版由张戈发布的代码,在部分主题的 functions.php 文件中添加后,可能会引发 PHP 警告错误。

报错信息示例:

Warning: preg_match_all() expects at least 3 parameters, 2 given in ...

出错原因

preg_match_all() 是 PHP 中用于执行全局正则表达式匹配的函数。其标准语法要求至少三个参数:

preg_match_all(pattern, subject, matches)

其中,matches 参数用于存储所有匹配的结果(数组)。原代码中缺少了这第三个必需的参数,导致函数调用不符合语法规范,从而触发警告。

修正方法

只需为代码中的两个 preg_match_all() 函数调用分别补上第三个参数(用于接收匹配结果的变量)即可。

原代码行:

if(!preg_match_all('/提交网址/u',$rs) && preg_match_all('/百度为您找到相关结果/u',$rs)){

修正后的代码行:

if(!preg_match_all('/提交网址/u',$rs,$mata) && preg_match_all('/百度为您找到相关结果/u',$rs,$matb)){

变量名 $mata$matb 可以自定义,它们仅用于接收匹配数据,在此逻辑中后续并未使用,因此任意合法的变量名均可。

完整修正代码

以下是修正后的完整函数代码,你可以将其复制并添加到主题的 functions.php 文件末尾。

/**
 * WordPress百度搜索自动推送、主动收录JS(修正版)
 */
add_action( 'wp_footer', 'bdPushData', 999);

// 检查百度是否已收录(改进版本)
if(!function_exists('baidu_check_record')){
  function baidu_check_record($url){
    global $wpdb;
    $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    $baidu_record  = get_post_meta($post_id,'baidu_record',true);
    if( $baidu_record != 1){
        $url='http://www.baidu.com/s?wd='.$url;
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        $rs=curl_exec($curl);
        curl_close($curl);
        // 如果抓取到的百度结果中不存在【提交网址】这个关键词,则认为该页面已被百度收录
        if(!preg_match_all('/提交网址/u',$rs,$mata) && preg_match_all('/百度为您找到相关结果/u',$rs,$matb)){
            update_post_meta($post_id, 'baidu_record', 1) || add_post_meta($post_id, 'baidu_record', 1, true);
            return 1;
        } else {
            return 0;
        }
    } else {
       return 1;
    }
  }
}

// 输出百度自动推送JS代码
if(!function_exists('bdPushData')){
  function bdPushData() {
    if ($_SERVER['HTTPS'] != "on") {
        $currentUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    } else {
        $currentUrl = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    }
    // 判断是否是未收录页面,并且当前页面是否等于正规url地址(get_permalink)
    if(baidu_check_record(get_permalink()) == 0 && $currentUrl == get_permalink()) {
        echo "<script>(function(){
            var bp = document.createElement('script');
            var curProtocol = window.location.protocol.split(':')[0];
            if (curProtocol === 'https') {
                bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
            } else {
                bp.src = 'http://push.zhanzhang.baidu.com/push.js';
            }
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(bp, s);
            })();</script>";
      }
   }
}

注意事项

  • 代码放置:请将代码添加到当前主题的 functions.php 文件末尾的 ?> 标签之前(如果该文件有结束标签的话)。
  • 功能逻辑:此代码会在网站前台页脚,为百度尚未收录的页面自动加载百度的“自动推送”JavaScript,以加速收录。
  • 收录状态缓存:代码会通过文章的自定义字段 baidu_record 缓存收录状态,避免每次访问都去查询百度,提升效率。
  • 环境要求:确保你的服务器 PHP 环境支持 curl 扩展,否则 baidu_check_record 函数将无法工作。

发表评论

您的邮箱不会公开。必填项已用 * 标注。