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

搜索引擎收录情况如果直接使用实时查询然后在文章或后台展示,会拖累网站加载速度。找了一个本地数据库版的,将查询结果保存到本地,从本地读取显示,当查询结果大于设定的时间后,自动进行查询结果更新。

本方法可实现查询结果自由显示,可在后台文章列表显示,也可在文章也显示,取舍可自行决定。
1、将下列代码放到主题的 functions 文件内。

/**
==================================================
*百度收录查询、本地保存、定时更新 
*黑鸟博客 https://www.guihet.com/
==================================================
**/

//在数据库创建自定义表,用于存储百度收录查询数据;
function my_table_install () {   
    global $wpdb;
    $table_name = $wpdb->prefix . "record";  //获取表前缀,并设置新表的名称
    if($wpdb->get_var("show tables like $table_name") != $table_name) {  //判断表是否已存在
        $sql = "CREATE TABLE " . $table_name . " (
	    id mediumint(9) NOT NULL AUTO_INCREMENT,
	    post_id bigint(20) UNSIGNED NOT NULL DEFAULT '0',
	    record_time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
	    record_name tinytext NOT NULL,
	    value text NOT NULL,
	    UNIQUE KEY id (id)
          );";
        require_once(ABSPATH . "wp-admin/includes/upgrade.php");  //引用wordpress的内置方法库
        dbDelta($sql);
    }
}
my_table_install ();
 
function checkBaidu($url) { 
    $url = 'https://www.baidu.com/s?wd=' . urlencode($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;}
}
function checkssl($url) { 
    $url = 'https://www.so.com/s?q=' . urlencode($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, '找不到该URL')) { //没有找到说明已被360收录 
        return 1; 
    }else{return 0;}
}
 
function baidurecord($surl){
    global $wpdb;
    $post_id =get_the_ID();
    $days = 7;
    $today = current_time('mysql'); //获取今天日期时间
    $daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) );  //Today - $days
    $results1 = $wpdb->get_results("SELECT * FROM wp_record where post_id = '$post_id' and record_name = 'baidu_record'");
    if(empty($results1)){
       if(checkBaidu($surl)==1 ){
	   $data = array('post_id'=> $post_id,'record_time'=> $today,'record_name'=> 'baidu_record','value'=> '1',);
           $wpdb->insert($wpdb->prefix ."record",$data);
	   return 1;
       }
	else {
	   $data = array('post_id'=> $post_id,'record_time'=> $today, 'record_name'=> 'baidu_record','value'=> '0',);
           $wpdb->insert($wpdb->prefix ."record",$data);
	   return 0;
       }
     }
     else {
        $results2 = $wpdb->get_results("SELECT value FROM wp_record WHERE record_time > '$daysago' and post_id = '$post_id' and record_name = 'baidu_record'");
           if(empty($results2)){
	     if(checkBaidu($surl)==1 ){
		$data1 = array('record_time'=> $today,'value'=> '1');
                $data2 = array('post_id'=> $post_id,'record_name' => 'baidu_record' );
             	$wpdb->update($wpdb->prefix ."record",$data1,$data2);
		return 1;
	     }else{
		$data1 = array('record_time'=> $today,'value'=> '0' );
             	$data2 = array('post_id'=> $post_id,'record_name' => 'baidu_record');
             	$wpdb->update($wpdb->prefix ."record",$data1,$data2);
		return 0;
	     }
	}
	else{			
	  if($results2[0]->value == 1){return 1;}
	  else{return 0;}
	}
    }
}
 
function sslrecord($slurl){
    global $wpdb;
    $post_id =get_the_ID();
    $days = 7;
    $today = current_time('mysql'); //获取今天日期时间
    $daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) );  //Today - $days
    $results1 = $wpdb->get_results("SELECT * FROM wp_record where post_id = '$post_id' and record_name = '360_record'");
    if(empty($results1)){
	if(checkssl($slurl)==1 ){
	   $data = array('post_id'=> $post_id,'record_time'=> $today, 'record_name'=> '360_record','value'=> '1',);
           $wpdb->insert($wpdb->prefix ."record",$data);
	   return 1;
	}
	else {
           $data = array( 'post_id'=> $post_id,'record_time'=> $today, 'record_name'=> '360_record','value'=> '0', );
           $wpdb->insert($wpdb->prefix ."record",$data);
	   return 0;
	}
    }
    else{	
        $results2 = $wpdb->get_results("SELECT * FROM wp_record WHERE record_time > '$daysago' and post_id = '$post_id' and record_name = '360_record'");
        if(empty($results2)){
	   if(checkssl($slurl)==1 ){
		$data1 = array('record_time'=> $today,'value'=> '1');
                $data2 = array('post_id'=> $post_id,'record_name' => '360_record' );
             	$wpdb->update($wpdb->prefix ."record",$data1,$data2);
		return 1;
	   }else
		{
		$data1 = array('record_time'=> $today,'value'=> '0' );
             	$data2 = array('post_id'=> $post_id,'record_name' => '360_record');
             	$wpdb->update($wpdb->prefix ."record",$data1,$data2);
		return 0;
	   }
	}
	else{
	    if($results2[0]->value == 1){return 1;}
	    else{return 0;}
	}
    }
}
 
function baidu_record() {
    if(baidurecord(get_permalink()) == 1) {
        echo '<a>百度已收录</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>';
   }
}
function ssl_record() {
    if(sslrecord(get_permalink()) == 1) {
        echo '<a>360已收录</a>';
   } else {
        echo '<a style="color:red;" rel="external nofollow noopener noreferrer" title="点击提交,谢谢您!" target="_blank" href="http://info.so.com/site_submit.html">360未收录</a>';
   }
}

《WordPress纯代码实现百度收录数据库保存及定时更新》
2、编辑当前主题下的文章模板(一般是single.php),在想要显示收录结果的位置添加如下代码并保存:

<?php echo baidu_record(); ?>

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

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

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

//1~ 在后台文章列表增加1列数据,展示百度收录情况
add_filter( 'manage_posts_columns', 'yy_customer_posts_columns' );
function yy_customer_posts_columns( $columns ) {
  $columns['baidurecord'] = '百度收录';
  $columns['baidurecord'] = '360收录';
  return $columns;
}
 
//2~ 输出查询结果
add_action('manage_posts_custom_column', 'yy_customer_columns_value', 10, 2);
function yy_customer_columns_value($column, $post_id){
    if($column=='baidurecord'){
	$baidurecord = $wpdb->get_var("SELECT value FROM wp_record WHERE post_id = '$post_id' and record_name = 'baidu_record'");
	if($baidurecord == 1) {
            echo Yes;
        }else{echo No;}
	}
    if($column=='sslrecord'){
	$sslrecord = $wpdb->get_var("SELECT value FROM wp_record WHERE post_id = '$post_id' and record_name = '360_record'");
	if($sslrecord == 1) {
            echo Yes;
        }else{echo No;}
	}
    return;
}

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

发表回复

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

  1. boke112联盟 说道:

    以前也折腾过查询百度是否收录,不收录就自动提交等,后来发现意义不大,一般收录的话都是发布文章的时候,所以后来就撤掉这个功能了。现在文中这个存放在数据库也挺方便的