无需插件实现WordPress网站自动推送文章给百度收录

建好 WordPress 站点之后,最期待的事情就是搜索引擎收录自己的站点,如何加速这一过程呢?对于国内用户来说,就是提交链接到百度。WordPress有很多这方面的插件,有收费的,也有免费的,但多多少少都带有其它不相干的功能,WordPress插件安装多了,影响网站的速度,给大家一个无插件实现WordPress网站自动推送文章给百度收录的方法。

方法非常简单,只需将这段代码添加到主题的 functions.php 文件或自定义插件中,然后在 WordPress 后台的 “设置” 菜单下找到 “百度站长推送” 进行配置即可。

// 注册设置页面
add_action('admin_menu', 'wpjam_baidu_zz_menu');
function wpjam_baidu_zz_menu() {
    add_options_page('百度站长推送设置', '百度站长推送', 'manage_options', 'baidu-zz-settings', 'wpjam_baidu_zz_settings_page');
}

// 显示设置页面
function wpjam_baidu_zz_settings_page() {
    if (!current_user_can('manage_options')) {
        wp_die(__('你没有足够的权限访问此页面。'));
    }
    
    // 保存设置
    if (isset($_POST['baidu_zz_api_url'])) {
        update_option('baidu_zz_api_url', sanitize_text_field($_POST['baidu_zz_api_url']));
        echo '<div class="updated"><p>设置已保存</p></div>';
    }
    
    $api_url = get_option('baidu_zz_api_url', '');
    ?>
    <div class="wrap">
        <h1>百度站长推送设置</h1>
        <form method="post">
            <table class="form-table">
                <tr>
                    <th scope="row"><label for="baidu_zz_api_url">API 地址</label></th>
                    <td>
                        <input type="text" name="baidu_zz_api_url" id="baidu_zz_api_url" 
                               value="<?php echo esc_attr($api_url); ?>" 
                               class="regular-text" 
                               placeholder="http://data.zz.baidu.com/urls?site=你的域名&token=你的token">
                        <p class="description">请在百度站长平台获取你的专属提交链接</p>
                    </td>
                </tr>
            </table>
            <?php submit_button('保存设置'); ?>
        </form>
    </div>
    <?php
}

// 保存文章时推送百度
add_action('save_post', 'wpjam_save_post_notify_baidu_zz', 10, 3);
function wpjam_save_post_notify_baidu_zz($post_id, $post, $update) {
    // 检查是否是自动保存
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    
    // 只处理发布状态的文章
    if ($post->post_status != 'publish') {
        return;
    }
    
    // 检查是否设置了API地址
    $baidu_zz_api_url = get_option('baidu_zz_api_url');
    if (empty($baidu_zz_api_url)) {
        error_log('百度站长推送API地址未设置');
        return;
    }
    
    // 获取文章链接
    $permalink = get_permalink($post_id);
    if (empty($permalink)) {
        error_log('无法获取文章链接,ID: ' . $post_id);
        return;
    }
    
    // 推送链接到百度
    $response = wp_remote_post($baidu_zz_api_url, array(
        'headers' => array(
            'Accept-Encoding' => '',
            'Content-Type' => 'text/plain'
        ),
        'sslverify' => true,  // 建议开启SSL验证,更安全
        'blocking' => false,
        'body' => $permalink
    ));
    
    // 记录推送结果
    if (is_wp_error($response)) {
        error_log('百度推送失败: ' . $response->get_error_message() . ',链接: ' . $permalink);
    } else {
        $response_body = wp_remote_retrieve_body($response);
        error_log('百度推送结果: ' . $response_body . ',链接: ' . $permalink);
    }
}

这段代码是一个 WordPress 钩子函数,用于在文章发布或更新时自动向百度站长平台推送链接。

  1. 在后台左侧功能菜单栏里的设置页面,找打百度站长推送,设置百度 API 地址,无需修改代码。
  2. 增加了错误处理和日志记录,方便排查问题。
  3. 避免了在自动保存时触发推送。
  4. 默认开启了 SSL 验证,提高安全性。
  5. 添加了更多的检查,确保推送过程更可靠。
打赏
Linux/Centos/Debian/Ubuntu系统更换yum源让下载速度起飞
上一篇
WordPress文章过多导致网站慢卡顿怎么优化
下一篇
博主索奈
Sonay
28 文章
0 评论
0 收藏
生成中...
二维码标题