Add META Tags to WordPress Head

By  on  

The reason that WordPress is so developer-friendly is their awesome hook system.  Depending on a post category (or seemingly anything else), you can modify the contents of the page, the theme templates, and everything else.  WordPress is amazing.

One recent use case I had was wanting to modify the <meta name="robots" content="{index? follow?}"> tag based on the post's category:  if the post is in category A, use noindex,nofollow, otherwise use index,follow.  After all not every post is a reflection of the overall site content.

Completing the task was easy using the WordPress hook system, specifically the wp_head hook:

// Worker function - echo tags to be added to the header
function dont_follow_some_posts() {
  global $IMPORTANT_CATEGORIES, $post;

  if(isset($post) && is_single($post) && in_category($IMPORTANT_CATEGORIES['sponsored'], $post->id)) {
    echo '';
  }
  else {
    echo '';
  }
}

// Hook into "wp_head" to add meta tags
add_filter('wp_head', 'dont_follow_some_posts');

You can use this hook to add anything to the site <head> but CSS and JavaScript files have their own methods so don't use this hook to add styles and scripts.

Take control of your WordPress instances by using the awesome hook system!

Recent Features

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

Discussion

  1. Hi David,

    This is cool. Instead of using SEO plugins we can use this to noindex and nofollow certain categories.

    By the way, What if I wanted to do this to tags?

    How do I do that?

    Regards.

  2. George

    Is there a reason to add index, follow even if its the default behaviour?

  3. This would be more easy for any developer to put directly into the code rather than using the category option in WordPress. But, Plugins in WordPress are also SEO friendly and mean a lot.

  4. Hello David, good tweak :)
    I would like to add that i ever you use this tweak, be sure that you are modifying a child theme and not directly on the theme himself. If you do not use child theme, on the next update, everything will be lost

  5. Nice one David,

    It might be worth mentioning that this needs to go in the themes ‘functions.php’ file.

    As Murat suggests, make sure you use a child theme otherwise all your changes vanish next time there’s an update. I always advise clients to use child themes (or create one for them when required) – https://codex.wordpress.org/Child_Themes has all the info you need…

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!