本文最后更新于2021年3月31日,已超过 4 年没有更新,如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
本文于2020年4月12日进行修正,并增加了360收录查询的保存及定时更新
网站文章实现百度收录的查询和显示网上的方法很多,有通过插件实现的,也有通过纯代码进行实现的,但都存在同样的问题:实时查询,一是加载速度会慢,二是可能被封服务器IP。所以我自己做了一个本地数据库版的,将查询结果保存到本地,从本地读取显示,当查询结果大于设定的时间后,自动进行查询结果更新。
本方法可实现查询结果自由显示,可在后台文章列表显示,也可在文章也显示,取舍可自行决定。
1、编辑WordPress主题functions文件,将下列代码放到?>标签之前。
/*
==================================================
*百度收录查询、本地保存、定时更新
*https://www.chyiyang.cn/139.html
==================================================
*/
//在数据库创建自定义表,用于存储百度收录查询数据;
function my_table_install () {
global $wpdb;
$table_name = $wpdb->prefix . "record"; //获取表前缀,并设置新表的名称
if($wpdb->get_var("show tables like $table_name") != $table_name) { //判断表是否已存在
$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
post_id bigint(20) UNSIGNED NOT NULL DEFAULT '0',
record_time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
record_name tinytext NOT NULL,
value text NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . "wp-admin/includes/upgrade.php"); //引用wordpress的内置方法库
dbDelta($sql);
}
}
my_table_install ();
function checkBaidu($url) {
$url = 'https://www.baidu.com/s?wd=' . urlencode($url);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$rs = curl_exec($curl);
curl_close($curl);
if (!strpos($rs, '提交网址')) { //没有找到说明已被百度收录
return 1;
}else{return 0;}
}
function checkssl($url) {
$url = 'https://www.so.com/s?q=' . urlencode($url);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$rs = curl_exec($curl);
curl_close($curl);
if (!strpos($rs, '找不到该URL')) { //没有找到说明已被360收录
return 1;
}else{return 0;}
}
function baidurecord($surl){
global $wpdb;
$post_id =get_the_ID();
$days = 7;
$today = current_time('mysql'); //获取今天日期时间
$daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) ); //Today - $days
$results1 = $wpdb->get_results("SELECT * FROM wp_record where post_id = '$post_id' and record_name = 'baidu_record'");
if(empty($results1)){
if(checkBaidu($surl)==1 ){
$data = array('post_id'=> $post_id,'record_time'=> $today,'record_name'=> 'baidu_record','value'=> '1',);
$wpdb->insert($wpdb->prefix ."record",$data);
return 1;
}
else {
$data = array('post_id'=> $post_id,'record_time'=> $today, 'record_name'=> 'baidu_record','value'=> '0',);
$wpdb->insert($wpdb->prefix ."record",$data);
return 0;
}
}
else {
$results2 = $wpdb->get_results("SELECT value FROM wp_record WHERE record_time > '$daysago' and post_id = '$post_id' and record_name = 'baidu_record'");
if(empty($results2)){
if(checkBaidu($surl)==1 ){
$data1 = array('record_time'=> $today,'value'=> '1');
$data2 = array('post_id'=> $post_id,'record_name' => 'baidu_record' );
$wpdb->update($wpdb->prefix ."record",$data1,$data2);
return 1;
}else{
$data1 = array('record_time'=> $today,'value'=> '0' );
$data2 = array('post_id'=> $post_id,'record_name' => 'baidu_record');
$wpdb->update($wpdb->prefix ."record",$data1,$data2);
return 0;
}
}
else{
if($results2[0]->value == 1){return 1;}
else{return 0;}
}
}
}
function sslrecord($slurl){
global $wpdb;
$post_id =get_the_ID();
$days = 7;
$today = current_time('mysql'); //获取今天日期时间
$daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) ); //Today - $days
$results1 = $wpdb->get_results("SELECT * FROM wp_record where post_id = '$post_id' and record_name = '360_record'");
if(empty($results1)){
if(checkssl($slurl)==1 ){
$data = array('post_id'=> $post_id,'record_time'=> $today, 'record_name'=> '360_record','value'=> '1',);
$wpdb->insert($wpdb->prefix ."record",$data);
return 1;
}
else {
$data = array( 'post_id'=> $post_id,'record_time'=> $today, 'record_name'=> '360_record','value'=> '0', );
$wpdb->insert($wpdb->prefix ."record",$data);
return 0;
}
}
else{
$results2 = $wpdb->get_results("SELECT * FROM wp_record WHERE record_time > '$daysago' and post_id = '$post_id' and record_name = '360_record'");
if(empty($results2)){
if(checkssl($slurl)==1 ){
$data1 = array('record_time'=> $today,'value'=> '1');
$data2 = array('post_id'=> $post_id,'record_name' => '360_record' );
$wpdb->update($wpdb->prefix ."record",$data1,$data2);
return 1;
}else
{
$data1 = array('record_time'=> $today,'value'=> '0' );
$data2 = array('post_id'=> $post_id,'record_name' => '360_record');
$wpdb->update($wpdb->prefix ."record",$data1,$data2);
return 0;
}
}
else{
if($results2[0]->value == 1){return 1;}
else{return 0;}
}
}
}
function baidu_record() {
if(baidurecord(get_permalink()) == 1) {
echo '<a>百度已收录</a>';
} else {
echo '<a style="color:red;" rel="external nofollow noopener noreferrer" title="点击提交,谢谢您!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'">百度未收录</a>';
}
}
function ssl_record() {
if(sslrecord(get_permalink()) == 1) {
echo '<a>360已收录</a>';
} else {
echo '<a style="color:red;" rel="external nofollow noopener noreferrer" title="点击提交,谢谢您!" target="_blank" href="http://info.so.com/site_submit.html">360未收录</a>';
}
}
数据库效果图
2、编辑当前主题下的文章模板(一般是single.php),在想要显示收录结果的位置添加如下代码并保存:
<?php echo baidu_record(); ?>
如果不想给其他人看到这个收录情况,可以加上一个判断语句,具体代码如下:
<?php if ( is_user_logged_in()){baidu_record();ssl_record();}?>
3、如果想在后台文章列表显示百度收录情况的话,将下来代码放到主题functions文件最后一行的?>之前。
//1~ 在后台文章列表增加1列数据,展示百度收录情况
add_filter( 'manage_posts_columns', 'yy_customer_posts_columns' );
function yy_customer_posts_columns( $columns ) {
$columns['baidurecord'] = '百度收录';
$columns['baidurecord'] = '360收录';
return $columns;
}
//2~ 输出查询结果
add_action('manage_posts_custom_column', 'yy_customer_columns_value', 10, 2);
function yy_customer_columns_value($column, $post_id){
if($column=='baidurecord'){
$baidurecord = $wpdb->get_var("SELECT value FROM wp_record WHERE post_id = '$post_id' and record_name = 'baidu_record'");
if($baidurecord == 1) {
echo Yes;
}else{echo No;}
}
if($column=='sslrecord'){
$sslrecord = $wpdb->get_var("SELECT value FROM wp_record WHERE post_id = '$post_id' and record_name = '360_record'");
if($sslrecord == 1) {
echo Yes;
}else{echo No;}
}
return;
}
后台文章列表效果图
好了,方法大致就是上面这些,代码可能存在bug,后续有时间再进行优化,写的不好敬请见谅~~
最新评论