WordPress纯代码实现百度收录数据库保存及定时更新

本文于2020年4月12日进行修正,并增加了360收录查询的保存及定时更新

网站文章实现百度收录的查询和显示网上的方法很多,有通过插件实现的,也有通过纯代码进行实现的,但都存在同样的问题:实时查询,一是加载速度会慢,二是可能被封服务器IP。所以我自己做了一个本地数据库版的,将查询结果保存到本地,从本地读取显示,当查询结果大于设定的时间后,自动进行查询结果更新。

本方法可实现查询结果自由显示,可在后台文章列表显示,也可在文章也显示,取舍可自行决定。

1、编辑WordPress主题functions文件,将下列代码放到?>标签之前。
// PHP CODE

0
/*
1
2
==================================================
3
*百度收录查询、本地保存、定时更新
4
5
*https://www.chyiyang.cn/139.html
6
7
==================================================
8
*/
9
10
//在数据库创建自定义表,用于存储百度收录查询数据;
11
function my_table_install () {
12
global $wpdb;
13
$table_name = $wpdb->prefix . "record”; //获取表前缀,并设置新表的名称
14
if($wpdb->get_var("show tables like $table_name”) != $table_name) { //判断表是否已存在
15
$sql = "CREATE TABLE ” . $table_name . ” (
16
id mediumint(9) NOT NULL AUTO_INCREMENT,
17
post_id bigint(20) UNSIGNED NOT NULL DEFAULT '0',
18
record_time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
19
record_name tinytext NOT NULL,
20
value text NOT NULL,
21
UNIQUE KEY id (id)
22
);”;
23
require_once(ABSPATH . "wp-admin/includes/upgrade.php”); //引用wordpress的内置方法库
24
dbDelta($sql);
25
}
26
}
27
my_table_install ();
28
29
function checkBaidu($url) {
30
$url = 'https://www.baidu.com/s?wd=' . urlencode($url);
31
$curl = curl_init();
32
curl_setopt($curl, CURLOPT_URL, $url);
33
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
34
$rs = curl_exec($curl);
35
curl_close($curl);
36
if (!strpos($rs, '提交网址')) { //没有找到说明已被百度收录
37
return 1;
38
}else{return 0;}
39
}
40
function checkssl($url) {
41
$url = 'https://www.so.com/s?q=' . urlencode($url);
42
$curl = curl_init();
43
curl_setopt($curl, CURLOPT_URL, $url);
44
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
45
$rs = curl_exec($curl);
46
curl_close($curl);
47
if (!strpos($rs, '找不到该URL')) { //没有找到说明已被360收录
48
return 1;
49
}else{return 0;}
50
}
51
52
function baidurecord($surl){
53
global $wpdb;
54
$post_id =get_the_ID();
55
$days = 7;
56
$today = current_time('mysql'); //获取今天日期时间
57
$daysago = date( "Y-m-d H:i:s”, strtotime($today) - ($days * 24 * 60 * 60) ); //Today - $days
58
$results1 = $wpdb->get_results("SELECT * FROM wp_record where post_id = '$post_id' and record_name = 'baidu_record'”);
59
if(empty($results1)){
60
if(checkBaidu($surl)==1 ){
61
$data = array('post_id'=> $post_id,'record_time'=> $today,'record_name'=> 'baidu_record','value'=> '1',);
62
$wpdb->insert($wpdb->prefix .”record”,$data);
63
return 1;
64
}
65
else {
66
$data = array('post_id'=> $post_id,'record_time'=> $today, 'record_name'=> 'baidu_record','value'=> '0',);
67
$wpdb->insert($wpdb->prefix .”record”,$data);
68
return 0;
69
}
70
}
71
else {
72
$results2 = $wpdb->get_results("SELECT value FROM wp_record WHERE record_time > '$daysago' and post_id = '$post_id' and record_name = 'baidu_record'”);
73
if(empty($results2)){
74
if(checkBaidu($surl)==1 ){
75
$data1 = array('record_time'=> $today,'value'=> '1');
76
$data2 = array('post_id'=> $post_id,'record_name' => 'baidu_record' );
77
$wpdb->update($wpdb->prefix .”record”,$data1,$data2);
78
return 1;
79
}else{
80
$data1 = array('record_time'=> $today,'value'=> '0' );
81
$data2 = array('post_id'=> $post_id,'record_name' => 'baidu_record');
82
$wpdb->update($wpdb->prefix .”record”,$data1,$data2);
83
return 0;
84
}
85
}
86
else{
87
if($results2[0]->value == 1){return 1;}
88
else{return 0;}
89
}
90
}
91
}
92
93
function sslrecord($slurl){
94
global $wpdb;
95
$post_id =get_the_ID();
96
$days = 7;
97
$today = current_time('mysql'); //获取今天日期时间
98
$daysago = date( "Y-m-d H:i:s”, strtotime($today) - ($days * 24 * 60 * 60) ); //Today - $days
99
$results1 = $wpdb->get_results("SELECT * FROM wp_record where post_id = '$post_id' and record_name = '360_record'”);
100
if(empty($results1)){
101
if(checkssl($slurl)==1 ){
102
$data = array('post_id'=> $post_id,'record_time'=> $today, 'record_name'=> '360_record','value'=> '1',);
103
$wpdb->insert($wpdb->prefix .”record”,$data);
104
return 1;
105
}
106
else {
107
$data = array( 'post_id'=> $post_id,'record_time'=> $today, 'record_name'=> '360_record','value'=> '0', );
108
$wpdb->insert($wpdb->prefix .”record”,$data);
109
return 0;
110
}
111
}
112
else{
113
$results2 = $wpdb->get_results("SELECT * FROM wp_record WHERE record_time > '$daysago' and post_id = '$post_id' and record_name = '360_record'”);
114
if(empty($results2)){
115
if(checkssl($slurl)==1 ){
116
$data1 = array('record_time'=> $today,'value'=> '1');
117
$data2 = array('post_id'=> $post_id,'record_name' => '360_record' );
118
$wpdb->update($wpdb->prefix .”record”,$data1,$data2);
119
return 1;
120
}else
121
{
122
$data1 = array('record_time'=> $today,'value'=> '0' );
123
$data2 = array('post_id'=> $post_id,'record_name' => '360_record');
124
$wpdb->update($wpdb->prefix .”record”,$data1,$data2);
125
return 0;
126
}
127
}
128
else{
129
if($results2[0]->value == 1){return 1;}
130
else{return 0;}
131
}
132
}
133
}
134
135
function baidu_record() {
136
if(baidurecord(get_permalink()) == 1) {
137
echo '<a>百度已收录</a>';
138
} else {
139
echo '<a style="color:red;" rel="external nofollow noopener noreferrer" title="点击提交,谢谢您!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'">百度未收录</a>';
140
}
141
}
142
function ssl_record() {
143
if(sslrecord(get_permalink()) == 1) {
144
echo '<a>360已收录</a>';
145
} else {
146
echo '<a style="color:red;" rel="external nofollow noopener noreferrer" title="点击提交,谢谢您!" target="_blank" href="http://info.so.com/site_submit.html">360未收录</a>';
147
}
148
}

数据库效果图

2、编辑当前主题下的文章模板(一般是single.php),在想要显示收录结果的位置添加如下代码并保存:
// PHP CODE

0
<?php echo baidu_record(); ?>

如果不想给其他人看到这个收录情况,可以加上一个判断语句,具体代码如下:

// PHP CODE

0
<?php if ( is_user_logged_in()){baidu_record();ssl_record();}?>

3、如果想在后台文章列表显示百度收录情况的话,将下来代码放到主题functions文件最后一行的?>之前。
// PHP CODE

0
//1~ 在后台文章列表增加1列数据,展示百度收录情况
1
add_filter( 'manage_posts_columns', 'yy_customer_posts_columns' );
2
function yy_customer_posts_columns( $columns ) {
3
$columns['baidurecord'] = '百度收录';
4
$columns['baidurecord'] = '360收录';
5
return $columns;
6
}
7
8
//2~ 输出查询结果
9
add_action('manage_posts_custom_column', 'yy_customer_columns_value', 10, 2);
10
function yy_customer_columns_value($column, $post_id){
11
if($column=='baidurecord'){
12
$baidurecord = $wpdb->get_var("SELECT value FROM wp_record WHERE post_id = '$post_id' and record_name = 'baidu_record'&#8221;);
13
if($baidurecord == 1) {
14
echo Yes;
15
}else{echo No;}
16
}
17
if($column=='sslrecord'){
18
$sslrecord = $wpdb->get_var("SELECT value FROM wp_record WHERE post_id = '$post_id' and record_name = '360_record'&#8221;);
19
if($sslrecord == 1) {
20
echo Yes;
21
}else{echo No;}
22
}
23
return;
24
}

后台文章列表效果图

好了,方法大致就是上面这些,代码可能存在bug,后续有时间再进行优化,写的不好敬请见谅~~

赞(1)
未经允许不得转载:伊阳博客 » WordPress纯代码实现百度收录数据库保存及定时更新
所属分类: WordPress

留言 抢沙发

  • QQ号
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

表情 关闭

图片