WordPress无需插件实现IndexNow提交搜索引擎促进抓取

建站技术62字数 945阅读3分9秒阅读模式

如果我们用过 IndexNow Plugin 插件的话应该知道在Bing搜索引擎工具中有这个插件可以提高必应和几个搜索引擎的抓取的,当然也不是所有的搜索引擎都识别。如果我们有需要做必应搜索推送的可以配合这个插件,当然我们也可以不用插件直接用代码实现。

//NoPlugin IndexNow
add_action('save_post','noplugin_indexnow',10,3);
function noplugin_indexnow($post_id, $post, $update){
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
    return;
}
if($post->post_status != 'publish'){
    return;
}
    if (get_post_meta($post_id, 'apipush', true) == "0") {
        return;
    }
$key = '自己获取KEY';
$api = 'https://www.bing.com/indexnow';
$url = get_permalink($post_id);
wp_remote_post( add_query_arg( ['url'=>$url,'key'=>$key], $api ), [
    'headers'   => ['Content-Type'=>'application/json; charset=utf-8'],
    'timeout'   => 10,
    'sslverify' => false,
    'blocking'  => true,
    'body'      => json_encode([
    'host'      => parse_url($url)['host'],
    'key'       => $key,
    'urlList'   => [$url]
    ])
]);
update_post_meta($post_id, "apipush", "0");
}

这里我们需要打开 :https://www.bing.com/indexnow/getstarted

然后看到这里。

这里我们获取KEY文件然后制作成对应的文件txt,丢到网站根目录后,然后再替换上面的代码 Key 后保存到我们的 Functions.php 中。

这里无插件可以替换成之前用过的 IndexNow 插件。

5/5 - (1 票)
 
麦子
  • 本文由 麦子 发表于 2024年4月23日 07:55:24
  • 转载请务必保留本文链接:https://www.idcxen.com/noplugin-indexnow.html