代码
将以下代码复制到主题 functions文件:
//WordPress IndexNow add_action('save_post','fanly_indexnow',10,3); function fanly_indexnow($post_id, $post, $update){ if($post->post_status!='publish') return; $key = '90cf913328b64b2eb6115994599e7788'; $api = 'https://api.indexnow.org/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] ]) ]); }
说明
$key: 8 – 128 个十六进制字符。密钥只能包含以下字符:小写字母(a-z),大写字母(A-Z),数字(0-9),以及短破折号(-)。
$api:https://api.indexnow.org/indexnow 或 https://www.bing.com/indexnow 或 https://yandex.com/indexnow 三选一,推荐第一个或第二个。获取key的方法
以上代码中$key 和$api 的值是可以修改的,同时$key 的值放置在当前网站根目录需要放置同名 txt 文件,例如:$key =4fe21022a6554c5ca88ee4beb621f9f4;那么需要建立一个4fe21022a6554c5ca88ee4beb621f9f4.txt 文件,且 txt 文件内容也为 4fe21022a6554c5ca88ee4beb621f9f4,放置在网站根目录。