WordPress 检测文章是否被百度收录

Gavin Wu 2021年03月21日 9:03 Wordpress 839 Views

直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function baidu_check($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(!strpos($rs,'没有找到')){
if( $baidu_record == 0){
update_post_meta($post_id, 'baidu_record', 1);
} else {
add_post_meta($post_id, 'baidu_record', 1, true);
}
return 1;
} else {
if( $baidu_record == false){
add_post_meta($post_id, 'baidu_record', 0, true);
}
return 0;
}
} else {
return 1;
}
}
function baidu_record() {
if(baidu_check(get_permalink()) == 1) {
echo '<a target="_blank" title="点击查看" rel="external nofollow noopener noreferrer" href="http://www.baidu.com/s?wd='.get_the_title().'">百度已收录</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>';
}
}

在需要的地方调用

1
<?php baidu_record(); ?>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Top