2020-11-01 / 1583阅
一个极简的百度主动推送demo代码,用于wordpress文章推动到百度。
<?php
/*
Plugin Name: 悠然自学网-简单的百度推送插件
Plugin URI: https://youranzixue.cn
Description: 一个简单的手动推送插件
Version: 1.0
Author: 悠然自学网
Author URI: https://youranzixue.cn
*/
add_filter('the_content', 'youran_add_button');
function youran_add_button($content)
{
if (is_single()) {
//开始推送
global $post;
$push = get_post_meta($post->ID,'push',true);
if(!$push){
$url = get_permalink();
$re = youran_baidu_push($url);
if($re->success ==1){
update_post_meta($post->ID,'push',1);
}
}
}
return $content;
}
function youran_baidu_push($url)
{
$urls = array($url);
$api = 'http://data.zz.baidu.com/urls?site=https://youranzixue.cn&token=你的秘钥';
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$result = json_decode($result);
return $result;
}
阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228