WordPress automatically detects whether the article is Baidu included - no need to plug a function to achieve

        We often see the blog heading some users have a small label, "Baidu has included", "Baidu is not included." Such a function in the end is how to achieve it? If we say that there must be a really useless, but if you need to add some friends personal blog this feature is still achievable. If there is a need to use a simple plug-in implementation with plug-ins, with no plug-ins here with a simple function to achieve.

First, code portion

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" href="http://www.baidu.com/s?wd='.get_the_title().'">百度已收录</a>';
} else {
echo '<a style="color:red;" rel="external nofollow" title="一键帮忙提交给百度,谢谢您!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'">百度未收录</a>';
}
}

Add the code to the current theme Functions.php file.

Second, call display

<?php baidu_record(); ?>

In the article page single.php currently heading to be displayed can be seen.

Section Source: www.ruanally.com

Guess you like

Origin blog.csdn.net/cnpinpai/article/details/90318355