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
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

Incredible Demos

  • By
    Instagram For MooTools

    If you're still rocking an iPhone and fancy taking a photo every now and then, you'd be crazy not to be using an app called Instagram.  With Instagram you take the photos just as you would with your native iPhone camera app, but Instagram...

  • By
    Create Spinning Rays with CSS3 Animations &#038; JavaScript

    Thomas Fuchs, creator of script2 (scriptaculous' second iteration) and Zepto.js (mobile JavaScript framework), creates outstanding animated elements with JavaScript.  He's a legend in his own right, and for good reason:  his work has helped to inspire developers everywhere to drop Flash and opt...

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!