一个 WordPress 百度搜索主动推送插件的实现,该插件可以帮助网站自动将新发布的内容推送到百度搜索,加快收录速度,同时支持手动推送历史文章。

- 插件功能说明
- 这个百度搜索主动推送插件具有以下核心功能:
- 自动推送:当新文章发布时,自动将文章 URL 推送到百度搜索,无需人工干预
- 手动推送:支持三种推送方式、推送所有未推送过的文章、推送最近 10 篇文章、手动输入自定义 URL 进行推送
- 推送日志:记录所有推送记录,包括成功 / 失败状态、时间和响应信息,方便追踪推送效果

百度 API 集成:与百度搜索资源平台的主动推送 API 对接,确保推送合规有效
使用方法
- 将插件文件上传到 WordPress 的wp-content/plugins/目录
- 在 WordPress 后台激活插件
- 进入 “设置 > 百度主动推送” 页面
- 输入从百度搜索资源平台获取的 API Token
- 选择是否启用自动推送功能
- 可根据需要进行手动推送操作

注意事项
- 需要先在百度搜索资源平台验证网站所有权并获取 API Token
- 百度对每日推送数量有配额限制,插件会显示剩余配额
- 推送成功不代表一定会被收录,仅表示百度已接收该链接
这个插件可以帮助 WordPress 网站更高效地将内容提交给百度搜索,有助于提高网站内容的收录速度和搜索引擎可见性。
附录(代码)
<?php
/*
Plugin Name: 百度搜索主动推送
Plugin URI: https://www.ipseo.net/
Description: 自动将新发布的文章推送到百度搜索,支持手动推送历史文章,加快收录速度。
Version: 1.0
Author: 解密SEO公司
Author URI: https://www.ipseo.net/
License: GPL2
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
// 激活插件时创建日志表
register_activation_hook(__FILE__, 'baidu_push_install');
function baidu_push_install() {
global $wpdb;
$table_name = $wpdb->prefix . 'baidu_push_logs';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
url text NOT NULL,
status tinyint(1) NOT NULL,
response text,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// 添加菜单
add_action('admin_menu', 'baidu_push_add_menu');
function baidu_push_add_menu() {
add_options_page(
'百度搜索主动推送设置',
'百度主动推送',
'manage_options',
'baidu-push',
'baidu_push_settings_page'
);
}
// 注册设置
add_action('admin_init', 'baidu_push_register_settings');
function baidu_push_register_settings() {
register_setting('baidu_push_group', 'baidu_push_token');
register_setting('baidu_push_group', 'baidu_push_auto');
}
// 设置页面
function baidu_push_settings_page() {
?>
<div class="wrap">
<h1>百度搜索主动推送设置</h1>
<form method="post" action="options.php">
<?php settings_fields('baidu_push_group'); ?>
<?php do_settings_sections('baidu_push_group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">百度推送API Token</th>
<td>
<input type="text" name="baidu_push_token" value="<?php echo esc_attr(get_option('baidu_push_token')); ?>" size="50" />
<p class="description">请输入百度搜索资源平台的推送API Token,可在<a href="https://ziyuan.baidu.com/" target="_blank">百度资源平台</a>获取。</p>
</td>
</tr>
<tr valign="top">
<th scope="row">自动推送</th>
<td>
<label>
<input type="checkbox" name="baidu_push_auto" value="1" <?php checked(1, get_option('baidu_push_auto'), true); ?> />
发布新文章时自动推送到百度
</label>
</td>
</tr>
</table>
<?php submit_button('保存设置'); ?>
</form>
<h2>手动推送</h2>
<form method="post" action="">
<input type="hidden" name="baidu_push_action" value="manual_push" />
<table class="form-table">
<tr valign="top">
<th scope="row">推送类型</th>
<td>
<select name="push_type">
<option value="all">所有未推送文章</option>
<option value="recent">最近10篇文章</option>
<option value="custom">自定义URL</option>
</select>
</td>
</tr>
<tr valign="top" id="custom_url_row" style="display:none;">
<th scope="row">自定义URL</th>
<td>
<textarea name="custom_urls" rows="5" cols="50" placeholder="每行一个URL"></textarea>
<p class="description">请输入要推送的URL,每行一个。</p>
</td>
</tr>
</table>
<?php submit_button('执行推送'); ?>
</form>
<h2>推送日志</h2>
<?php baidu_push_display_logs(); ?>
</div>
<script>
document.querySelector('select[name="push_type"]').addEventListener('change', function() {
document.getElementById('custom_url_row').style.display = this.value === 'custom' ? 'table-row' : 'none';
});
</script>
<?php
}
// 处理手动推送
add_action('admin_init', 'baidu_push_handle_manual_push');
function baidu_push_handle_manual_push() {
if (isset($_POST['baidu_push_action']) && $_POST['baidu_push_action'] === 'manual_push' && current_user_can('manage_options')) {
$token = get_option('baidu_push_token');
if (empty($token)) {
wp_die('请先设置百度推送API Token');
}
$urls = array();
$push_type = sanitize_text_field($_POST['push_type']);
switch ($push_type) {
case 'all':
$urls = baidu_push_get_all_unpushed_urls();
break;
case 'recent':
$urls = baidu_push_get_recent_urls(10);
break;
case 'custom':
$custom_urls = sanitize_textarea_field($_POST['custom_urls']);
$urls = array_filter(explode("\n", $custom_urls), 'trim');
break;
}
if (!empty($urls)) {
$result = baidu_push_send_urls($token, $urls);
baidu_push_log_results($urls, $result);
if ($result['success']) {
wp_redirect(add_query_arg('push_success', 1, admin_url('options-general.php?page=baidu-push')));
exit;
} else {
wp_redirect(add_query_arg('push_error', $result['message'], admin_url('options-general.php?page=baidu-push')));
exit;
}
}
}
}
// 自动推送新文章
add_action('publish_post', 'baidu_push_auto_publish');
function baidu_push_auto_publish($post_id) {
if (get_option('baidu_push_auto') == 1) {
$token = get_option('baidu_push_token');
if (!empty($token)) {
$url = get_permalink($post_id);
$result = baidu_push_send_urls($token, array($url));
baidu_push_log_results(array($url), $result);
}
}
}
// 获取所有未推送的文章URL
function baidu_push_get_all_unpushed_urls() {
global $wpdb;
$table_name = $wpdb->prefix . 'baidu_push_logs';
$posts = get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => -1,
));
$urls = array();
foreach ($posts as $post) {
$url = get_permalink($post->ID);
$count = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE url = %s",
$url
));
if ($count == 0) {
$urls[] = $url;
}
}
return $urls;
}
// 获取最近的文章URL
function baidu_push_get_recent_urls($num = 10) {
$posts = get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => $num,
));
$urls = array();
foreach ($posts as $post) {
$urls[] = get_permalink($post->ID);
}
return $urls;
}
// 发送URL到百度
function baidu_push_send_urls($token, $urls) {
$api_url = 'http://data.zz.baidu.com/urls?site=' . home_url() . '&token=' . $token;
$ch = curl_init();
$options = array(
CURLOPT_URL => $api_url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$result = json_decode($response, true);
if ($http_code == 200 && isset($result['success']) && $result['success'] > 0) {
return array(
'success' => true,
'message' => "成功推送 " . $result['success'] . " 条URL,剩余配额:" . $result['remain'],
'data' => $result
);
} else {
$error_msg = isset($result['message']) ? $result['message'] : "推送失败,HTTP状态码:" . $http_code;
return array(
'success' => false,
'message' => $error_msg,
'data' => $result
);
}
}
// 记录推送结果
function baidu_push_log_results($urls, $result) {
global $wpdb;
$table_name = $wpdb->prefix . 'baidu_push_logs';
if ($result['success']) {
foreach ($urls as $url) {
$wpdb->insert($table_name, array(
'url' => $url,
'status' => 1,
'response' => json_encode($result['data'])
));
}
} else {
foreach ($urls as $url) {
$wpdb->insert($table_name, array(
'url' => $url,
'status' => 0,
'response' => $result['message']
));
}
}
}
// 显示推送日志
function baidu_push_display_logs() {
global $wpdb;
$table_name = $wpdb->prefix . 'baidu_push_logs';
$logs = $wpdb->get_results("SELECT * FROM $table_name ORDER BY created_at DESC LIMIT 50");
if (empty($logs)) {
echo '<p>暂无推送记录</p>';
return;
}
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr><th>URL</th><th>状态</th><th>时间</th><th>响应</th></tr></thead>';
echo '<tbody>';
foreach ($logs as $log) {
$status = $log->status ? '<span style="color:green">成功</span>' : '<span style="color:red">失败</span>';
echo '<tr>';
echo '<td><a href="' . esc_url($log->url) . '" target="_blank">' . esc_url($log->url) . '</a></td>';
echo '<td>' . $status . '</td>';
echo '<td>' . $log->created_at . '</td>';
echo '<td><pre>' . esc_html($log->response) . '</pre></td>';
echo '</tr>';
}
echo '</tbody></table>';
}
// 添加设置链接
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'baidu_push_add_action_links');
function baidu_push_add_action_links($links) {
$settings_link = '<a href="options-general.php?page=baidu-push">设置</a>';
array_unshift($links, $settings_link);
return $links;
}

微信扫一扫打赏
支付宝扫一扫打赏
