最近看到很多站点上都添加了检测百度是否收录的功能,这极大地方便了站长本人对网站内容是否收录的管理,同时可以更加亲密地与访客进行交互,自己闲来无事也给自己网站添加了这一功能,以下是我站点添加此功能的记录,希望能帮助到同样需要此功能的朋友们。

方法一:
下面只需一段代码即可随时了解百度收录情况,并提供提交百度入口,以加速百度收录。
//百度收录提示
if (git_get_option('git_baidurecord_b') && function_exists('curl_init')) {
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 title="" href="https://www.baidu.com/s?wd=' . get_the_title() . '" target="_blank"
rel="external nofollow" data-original-title="点击查看">已收录</a>';
} else {
echo '<a style="color: red;" title="" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename=' . get_permalink() . '" target="_blank"
rel="external nofollow" data-original-title="点击提交,谢谢您!">未收录</a>';
}
}
}
上面的函数式放在主题 functions 文件中,然后在文章内容页面适合地方加入下方这段代码
<?php if (git_get_option('git_baidurecord_b') && function_exists('curl_init')) { ?><span class="muted"><i class="fa fa-flag"></i> <?php
baidu_record(); ?></span>
方法二:
只有管理员才能在站台看到该文章是否被百度收录的提示,其它的访客是完全看不到的,而且本代码有自动的未收录提示功能,如果我们点击未收录提示字样,可以直接链接到提交百度站长链接的页面,主动向百度提交我们未被收录的文章链接,而且操作成本也很低,只需要把以下的代码复制到我们当前的 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) {
echo '<p align=right><b><a target="_blank" title="点击查看" rel="external nofollow" href="https://www.baidu.com/s?wd=' . get_the_title() . '">此文章已被百度收录</a></b>(仅管理员可见)</p>';
} else {
$content = "<p align=right><b><a style=color:red target=_blank href=https://zhanzhang.baidu.com/sitesubmit/index?sitename=" . get_permalink() . ">百度未收录!点击此处一键提给百度交</a></b>(仅管理员可见)</p>" . $content;
}
}
return $content;
}
以上2种版本可根据个人喜好以及主机实际情况选择一个即可!
本文已通过「原本」原创作品认证,转载请注明文章出处及链接。



