Integrate IndexNow with WordPress, using the “save_post” hook

Share this:

For today’s post, we’re back on the topic of SEO—specifically implementing IndexNow for WordPress. 

Those of you who work in SEO will no doubt be aware of what IndexNow is, but for those of you who don’t, the first thing we’ll do is provide a brief overview of what it is and why it’s worth adding to your existing WordPress site.

It’s going to be a relatively short one, as implementing this into your site isn’t particularly difficult. And as always, I’ve included the code and steps you’ll need to follow to get it up and running. 

Right, now with the intro out of the way, let’s get started—what is IndexNow?

What is IndexNow

Index now is an initiative from Microsoft that enables you to ping search engines, requesting that pages of a website are indexed in real-time as and when changes to them are made. It’s essentially the Google Indexing API, but without the limitation of being for job postings and embedded video.

Right now, there are only a handful of search engines that use it—the most noteworthy of which are Bing and Yandex. Unfortunately, and while they’ve indicated that they are testing it, Google have yet to adopt IndexNow and haven’t said much about it since late last year.

That said, while sure, without Google signed up it’s not anywhere near as useful as it could be, it still never hurts to speed up site indexing wherever you can, even if it is only Bing, Yandex, and a handful of other, arguably obscure search engines currently.

 You can learn more about IndexNow on their official website and in their developer documentation.

How it works

IndexNow is essentially a pinging service – once a page is updated, you let them know – either with a GET request for single pages, or a POST for bulk indexing.

Our code – which will be given in the “Setting it up” section of this article—simply creates the URL needed to ping IndexNow, based on the permalink of the created or updated page, and performs a simple GET request with cURL.

In addition to the above, there’s also an extra couple of lines of code to create a simple log file, which while optional, should be used to check that you’ve got everything up and running correctly. I’ve included this below for reference:

        $file = 'indexnow_log.txt';
	$date = date("m/d/Y h:i:s a", time());
	$logstring = "Page: ".$link." Status code: ".$http_status." Datetime: ".$date."\n";
	file_put_contents($file, $logstring, FILE_APPEND | LOCK_EX);

Setting it up

  1. You’ll first need an API key—which you can get from Bing’s IndexNow page.


  2. Create a text file—it’s name and contents should be your key—see example below:


  3. Upload this file to the root of your website
  4. In your theme’s functions.php file, add the following code:

functions.php

add_action('save_post', 'index_now');

function index_now($post_id){
	$link = get_permalink($post_id);
	$key = 'YOUR_INDEXNOW_KEY';
	$url = "https://api.indexnow.org/indexnow?url=".$link."&key=".$key;
	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); 
	curl_setopt($curl, CURLOPT_TIMEOUT, 400);
	curl_exec($curl);
	$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
	curl_close($curl);
}

That’s it—now every time you publish or update a page or post, you’ll ping the IndexNow endpoint, requesting indexing. 

If you don’t want to do all that – there’s an official plugin

IndexNow offical plugin page on wordpress.org

Personally, I like to avoid using plugins where I can—and given that the same can be achieved with a few lines of code, it doesn’t make a lot of sense to me to use a plugin for it. 

That said, if you’re more comfortable with uploading a plugin than editing your site’s functions.php file, you can get the IndexNow WordPress plugin here.

Summit it up

Well, that’s it—a relatively short post today. IndexNow is a great initiative, as ensuring that the latest version of your pages are indexed is not just great for you, it’s also great for users. 

While it’s yet to be seen if Google take the plunge and get onboard, it’s likely that if they don’t that they’ll release something similar, as indexing is an issue that many of us face, and one that seems to be growing. 

Google have already released the Indexing API—something mentioned earlier in this post, and a topic I’ll no doubt cover in a future post – but this is limited to job postings and embedded video only. Logically, it would make sense that they’ll look to widen the scope of this feature to include other forms of content and eventually web pages.

If you have any questions or issues with the code provided in this post, reach out to me in the comments and I’ll do my best to respond in order to help you get it up and running. Thanks for reading!

Share this:

Leave a Comment