关于wordpress定时提交链接到百度站长的功能实现 writeor的博客 wr的小窝喔~
  • 欢迎访问wr的小窝~,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站.
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧
  • 嘟嘟嘟嘟嘟嘟啦~~

关于wordpress定时提交链接到百度站长的功能实现

未分类 writeor 1年前 (2024-03-29) 136次浏览 已收录 0个评论
百度SEO的普通收录好像现在每条只能推10个链接,我们通过在主题的functions.php文件中添加代码来实现定时推送的功能。

代码会先获取当前系统的所有文章,然后随机取出10个去推送,并把结果存到数据库,之后可以再取出来显示记录

//定时运行提交链接到百度/每天10条
// 注册一个WP-Cron事件
function custom_cron_job() {
    if ( ! wp_next_scheduled( 'custom_cron_action' ) ) {
        wp_schedule_event( time(), 'daily', 'custom_cron_action' );
    }
}
add_action( 'wp', 'custom_cron_job' );

// 执行的代码
function custom_cron_function() {
    $urls = array();
    $args = array(
        'post_type' => 'post', 
        'posts_per_page' => -1, 
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            $urls[] = get_permalink();
        }
    }
    wp_reset_postdata();

    $random_urls = array_rand($urls, 10);
    $selected_urls = array();
    foreach ($random_urls as $index) {
        $selected_urls[] = $urls[$index];
    }

    $api = '你的api链接'; 
    $ch = curl_init();
    $options =  array(
        CURLOPT_URL => $api,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => implode("\n", $selected_urls),
        CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
    );
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    $timestamp = current_time( 'mysql' ); // 获取当前时间戳
	// 存储结果和运行时间在数据库中
    $data = array(
        'result' => $result,
        'timestamp' => $timestamp,
    );
    add_option( 'custom_cron_result_history', $data, '', 'no');
}
add_action( 'custom_cron_action', 'custom_cron_function' );

可以使用下面的代码来显示推送的记录

反序列化还有些问题但凑合能看,时间戳也不对劲。

$history = get_option( 'custom_cron_result_history' );
if ( $history ) {
    foreach ( $history as $serializedData ) {
        $data = maybe_unserialize($serializedData); // 反序列化数据
        if ($data !== false) {
            // 输出反序列化后的内容
            echo '<pre>';
            var_dump($data); // 或者使用 print_r($data);
            echo '</pre>';
        } else {
            // 解析失败的处理逻辑
            echo "Unserialization failed for data: $serializedData";
        }
    }
}

 


wr的小窝 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于wordpress定时提交链接到百度站长的功能实现
喜欢 (0)
[1528532472@qq.com]
分享 (0)

您必须 登录 才能发表评论!

wpChatIcon
wpChatIcon