✅ 使用步骤一览:
第一步:获取你的 主动推送 API 接口地址
登录百度搜索资源平台:
👉 https://ziyuan.baidu.com/linksubmit/index
绑定站点后,会生成如下格式的 API 地址:
http://data.zz.baidu.com/urls?site=你的域名&token=你的推送token
外贸网站优化
第二步:将以下代码添加到你的 WordPress 主题 functions.php 文件中(建议使用子主题)
// WordPress 自动推送百度收录 API
function push_post_to_baidu($post_ID) {
// 判断是否为发布新文章(可按需调整 post type)
$post_url = get_permalink($post_ID);
$api = 'http://data.zz.baidu.com/urls?site=yourdomain.com&token=your_token'; // ← 替换为你自己的 API 地址
// 检查文章是否为已发布状态
if (get_post_status($post_ID) == 'publish') {
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $post_url,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
// 可选:记录日志
error_log("百度推送结果:".$result);
}
return $post_ID;
}
add_action('publish_post', 'push_post_to_baidu'); // 针对文章发布
🔒 安全提醒:
yourdomain.com
和your_token
要替换成你实际绑定的站点和 API Token。- 推荐使用子主题或站点专属插件来写这段代码,避免主题升级时丢失。
- 百度并不保证推送必收录,但推送后会大大提高抓取率和速度。
✅ BONUS:支持多个 URL 一起推送?
如果你想一次性推送多篇文章(如更新旧内容),可以扩展为数组批量推送:
$post_urls = array(
get_permalink($post_ID),
// 可添加更多链接
);
$post_body = implode("\n", $post_urls);
然后将 $post_url
改为 $post_body
即可。