add_action( 'wp_footer', 'bdPushData', 999); if(!function_exists('baidu_check_record')){ function baidu_check_record($url,$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( BD_PUSH == 'yes' && !preg_match_all('/提交网址/u',$rs) && preg_match_all('/百度为您找到相关结果/u',$rs)){ 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; } } } if(!function_exists('bdPushData')){ function bdPushData() { global $wpdb; $post_id = ( null === $post_id ) ? get_the_ID() : $post_id; $currentUrl = home_url(add_query_arg(array())); //这里修改了下:给get_permalink指定了文章ID if(baidu_check_record(get_permalink($post_id), $post_id) == 0 && $currentUrl == get_permalink($post_id)) { 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); })(); (function(){ var src = (document.location.protocol == 'http:') ? 'http://js.passport.qihucdn.com/11.0.1.js?af9e600e6a4ba6d33cd7f1b088210cf7':'https://jspassport.ssl.qhimg.com/11.0.1.js?af9e600e6a4ba6d33cd7f1b088210cf7'; document.write('<script src=\"' + src + '\" id=\"sozz\"><\/script>'); })();</script>"; } } }
将上述代码添加到functions.php即可,当页面未被百度收录,且被访问的页面地址等于WordPress唯一页面地址时,将会输出百度自动推送js代码,不符合条件的页面则不会输出。
如果更改主题代码后出现以下错误
Uncaught Error: Call to undefined function curl_init()
则执行安装php-curl
sudo apt-get install php-curl
并重启一下apache
sudo service apache2 restart
- 本文固定链接: https://freemanapp.com/zh/?p=823
- 转载请注明: freeman 于 FreemanApp 发表