WordPress Plugin – Meta Robots

WordPress Plugin – Meta Robots

Mini project of the day… I did create a tiny WordPress plugin, to add a meta robots header to some of my pages, to prevent them from being indexed by google and other search engines.

So without further ado, here’s the source code, to be put in a file: wp-content/plugins/meta-robots/meta_robots.php in your WordPress installation folder:

<?php
/*
Plugin Name: Meta Robots
Plugin URI: https://www.kaper.com/other/wordpress-plugin-meta-robots/
Description: Copies value of custom field "meta-robots" (in post or page) as meta robots tag in your post's head section. Example value "noindex" to prevent google indexing.
Version: 1.0
Author: Thijs Kaper
Author URI: https://www.kaper.com/thijs
*/

function meta_robots_header() {
  global $post;
  $robotValue = get_post_meta($post->ID, "meta-robots", true); 
  if ($robotValue) {
    echo "<meta name=\"robots\" content=\"".$robotValue."\">\n";
  }
}

add_action('wp_head', 'meta_robots_header');
?>
Code language: HTML, XML (xml)

After adding this code, go to your plugin configuration, and enable the plugin. It should show up in the list automatically.

To enable the header, find a post or page, and make sure to enable showing of the “Custom Fields” entry form (top right of your page -> click the tripple-dot menu -> click “Options” -> check “Custom Fields”. The “Custom Fields” entry form is shown below your post editor. Add a custom field with name “meta-robots”, and value “noindex” if you want to prevent google (and other search engines) from indexing your page.

That’s all there is to it… Of course you can also use any other robots meta values, “index” / “follow” / “nofollow” / “noindex”. Just look up the specs for it, and when to use what.

Why would you (read “I”) want to use a “noindex” marker? In my case, I have some test pages on the site, a personalized start page for myself, and a page detailing the content of a bet I placed with a friend (about when we expect self-driving cars to be commonly available), which is of no real interest to the rest of the world 😉 Even though there are no active links to these pages, google always seems to find a way to get their address (sniffing -> “personalizing”; facebook messenger, whatsapp, gmail, google calendar, or whatever?). So let’s just tell google to NOT store the contents, in case it stumbles on it.

p.s. To show this plugin works, I have added a value of “follow, index” to this post page 😉 Just view the page source to check, or for unix command liners; use curl with grep on robots.

Thijs.

2 thoughts on “WordPress Plugin – Meta Robots

    1. Hi Marc, In itself a good question, if you just read half of the article, ha-ha. The answer is in there somewhere 😉

Leave a Reply

Your email address will not be published. Required fields are marked *