Tell Bing, Yandex and other IndexNow search engines about new and updated posts the moment you publish, with a key file WordPress serves for you.
/**
* IndexNow: tell participating search engines (Bing, Yandex, Seznam, Naver and others)
* about new and updated URLs as soon as they are published.
*/
function nahnu_indexnow_key(): string {
$key = (string) get_option( 'nahnu_indexnow_key' );
if ( '' === $key ) {
$key = wp_generate_password( 32, false, false );
update_option( 'nahnu_indexnow_key', $key, false );
}
return $key;
}
// Serve the key file at /{key}.txt so search engines can confirm you own the site.
add_action( 'init', function (): void {
$key = nahnu_indexnow_key();
$expected = trim( (string) wp_parse_url( home_url( '/' . $key . '.txt' ), PHP_URL_PATH ), '/' );
$request = trim( (string) wp_parse_url( $_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH ), '/' );
if ( $request === $expected ) {
header( 'Content-Type: text/plain; charset=utf-8' );
echo esc_html( $key );
exit;
}
} );
// Ping IndexNow when a public post is published or updated.
add_action( 'transition_post_status', function ( string $new_status, string $old_status, WP_Post $post ): void {
if ( 'publish' !== $new_status || wp_is_post_revision( $post ) || ! is_post_type_viewable( $post->post_type ) ) {
return;
}
if ( 'production' !== wp_get_environment_type() || get_transient( 'nahnu_indexnow_' . $post->ID ) ) {
return;
}
set_transient( 'nahnu_indexnow_' . $post->ID, 1, MINUTE_IN_SECONDS );
$key = nahnu_indexnow_key();
wp_remote_post( 'https://api.indexnow.org/indexnow', [
'blocking' => false,
'timeout' => 5,
'headers' => [ 'Content-Type' => 'application/json; charset=utf-8' ],
'body' => wp_json_encode( [
'host' => wp_parse_url( home_url(), PHP_URL_HOST ),
'key' => $key,
'keyLocation' => home_url( '/' . $key . '.txt' ),
'urlList' => [ get_permalink( $post ) ],
] ),
] );
}, 10, 3 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
This snippet used to ping Google’s sitemap address on publish. Google shut that address down in 2023, so the ping did nothing. The modern replacement is IndexNow, an open protocol that Bing, Yandex, Seznam, Naver and other search engines share: you tell one of them a URL has changed and they all hear about it within minutes.
IndexNow needs a key so search engines can confirm the ping really came from your site. The first time the snippet runs it creates a random 32-character key and saves it as an option. It then answers requests for /your-key.txt with that key, so you don’t have to upload a file.
When any public post type is published or updated, the transition_post_status hook sends a small JSON request to api.indexnow.org with the post’s address. The request is non-blocking, so publishing is never slowed down, and a one-minute transient stops the block editor’s double save from sending two pings.
Pings only go out when wp_get_environment_type() is production, which is the default. Set WP_ENVIRONMENT_TYPE to staging in wp-config.php on test sites so they never announce staging URLs.
Google doesn’t use IndexNow. It finds new content through your sitemap at /wp-sitemap.xml and normal crawling, so submit that sitemap once in Google Search Console and leave it there.
Visit yoursite.com/ followed by the key and .txt. You can see the key under nahnu_indexnow_key in your options table, or run wp option get nahnu_indexnow_key. The page should show only the key. After publishing a post, Bing Webmaster Tools lists it under IndexNow within a day.