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
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

  • 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...

Incredible Demos

  • By
    MooTools Clipboard Plugin

    The ability to place content into a user's clipboard can be extremely convenient for the user. Instead of clicking and dragging down what could be a lengthy document, the user can copy the contents of a specific area by a single click of a mouse.

  • By
    Create a Twitter AJAX Button with MooTools, jQuery, or Dojo

    There's nothing like a subtle, slick website widget that effectively uses CSS and JavaScript to enhance the user experience.  Of course widgets like that take many hours to perfect, but it doesn't take long for that effort to be rewarded with above-average user retention and...

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!