WordPress运维日记-给WordPress文章加百度是否已收录功能

前言

最近发现好多博客都有这个功能,所以我也来跟个风。

教程

法一:

编辑wordpress主题目录下的functions.php文件,在最后一个?>标签前新添如下代码并保存。如果没有?>标签,则直接加到最后面。

/*
判断当前文章是否被百度收录,若没有被收录则可点击提交至百度,加速收录!(此插件在文章页面仅管理员可见)
*/
function d4v($url){
$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,’没有找到’)){
return 1;
}else{
return 0;
}
}
add_filter( ‘the_content’, ‘baidu_submit’ );
function baidu_submit( $content ) {
if( is_single() && current_user_can( ‘manage_options’) )
if(d4v(get_permalink()) == 1)
$content=”

百度已收录(仅管理员可见)

“.$content;
else
$content=”

<a style=color:red target=_blank href=http://zhanzhang.baidu.com/sitesubmit/index?sitename=".get\_permalink().">百度未收录!点击此处提交(仅管理员可见)

“.$content;
return $content;
}

法二: 同样是在functions.php文件下添加如下代码。

/*
判断当前文章是否被百度收录,(此插件在文章页面所有人可见)
*/
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 ‘百度已收录‘;
} else {
echo ‘百度未收录‘;
}
}

再编辑文章模板(一般是single.php),在合适的位置添加如下代码并保存:

结尾

用起来还不错。