Two Easy Ways to Add “nofollow” to WordPress Menu Items

By default, WordPress menus don’t have the ability to add “nofollow” to the link items…but WordPress 3.0+ has the functionality built in.

This tutorial will show you how to add nofollow to specific items using the new wp_nav_menu() function.

Enable nofollow in WordPress 3.0 nav menus

1. Click the “Screen Options” tab

2. Check the “Link Relationship (XFN) box

Notice all the other options for menu items (posts, tags aren’t enabled by default) and menu item properties (link targets, classes, descriptions).

3. Enter “nofollow” as the Link Relationship (XFN)

Then save the menu. Voila!

There’s an alternative way to do this as well.

You can add this to the functions.php

Using the code below, you can automate this whole process, which would be nice if you want to do this all at once.

You can also specify only to add nofollow to only one menu location or one menu.

add_filter('walker_nav_menu_start_el', 'nofollow_menu_items', 1, 4);
function nofollow_menu_items($item_output, $item, $depth, $args) {
	$nofollow = array(105,268); // Menu item id's (View page source and menu-item-123)
	$location = ''; // Use 'primary' to only filter header menu in twentyten
	$menu = ''; // Use menu names to filter by menu

	if(
		in_array($item->ID, $nofollow)
		&& (!empty($location) && $args->theme_location == $location || empty($location))
		&& (!empty($menu) && $args->menu == $menu || empty($menu))
	) {
		$item_output = str_replace('<a ', '<a rel="nofollow" ', $item_output);
	}

	return $item_output;
}
This entry was posted in Tutorial, WordPress and tagged , , , , . Bookmark the permalink.
  • http://www.dragonblogger.com Justin Germino

    Very useful article and I just changed my menu categories to nofollow but left my pages as dofollow so they can get the PR juice. Thanks for this, I stumbled and shared with my Twitter audience.

    • Stankovicuros

      I dont now why, but for me not working. I use flexsqueze theme.

  • http://www.FreeTravelGenius.com Dave

    I am implementing it on my website now.  Thank you

  • http://evanrmurphy.com Evan R. Murphy

    Great tip, thank you.

  • pozycjonowanie częstochowa

    Thank’s for tips. It’s very helpful. :)

  • http://eliteeternity.com eliteeternity

    woohoo thanks

  • http://www.outsourcing-partners.com/wordpress-developer.html Gini

    Helpful tips, thanks to share with us.

  • http://www.techcrates.com Salman @ Tech Blog

    Great tute … bro !! Will be helpful in preserving the link juice to pass on to about and contact pages .. Thanks for the share 

  • http://meeble.com meeble

    wow, you taught me something VERY useful in this post.  ok, double WOW.  I had no idea that those options were hiding there under the Screen Options.  Thank you very, very much.  You just saved me a tiny bit of link juice on:  http://meeble.com

    I owe you one.  :)

  • http://www.itrush.com/ IT Rush

    Been looking for solution on this thing, thanks for sharing this technique.

  • http://www.hindisms4u.com/ Hindi SMS

    Link Relationship option is not available in my wordpress dashboard. What to do?

    • http://www.seodenver.com Zack Katz

      Did you read Step 1?

  • Kristoffer Adrian Amora

    Wow! This solved my problem! I never knew what Screen Options was for until I read your post. Thank you!

    • http://www.seodenver.com Zack Katz

      You’re most welcome :-)

  • Pauline Taylor

    This attribute is very much useful but is it compatible with other lower versions too?

  • Knuth Becker

    Great tip! Don’t see why wordpress don’t show this as default :-)

    • http://www.facebook.com/lars.koudal Lars Koudal

      I think because it would confuse most users who would not understand what it is or how to fill it out.

  • Pauline Taylor

    I also want to know ‘This attribute is very much useful but is it compatible with other lower versions too?’

    • http://www.seodenver.com Zack Katz

      I believe it’s compatible from 2.8+

  • http://howtogetridoffruitfliestips.com/ Shipon

    It’s a nice tips.thanks for share…. Keep it up man

  • ladybug

    Is there a way to make the code above work with the custom menu widget? I tried adding it to my function.php file but did not see the nofollow attribute on my menu widgets.

    • ladybug

      This code works with widgets:

      /* Add No Follow to Menu Links */

      add_filter( ‘wp_nav_menu_objects’, ‘menu_rel_nofollow’, 10, 2 );
      function menu_rel_nofollow( $items, $args )
      $item->xfn = ‘nofollow’;

      return $items;

      add_filter(‘widget_text’, ‘text_rel_nofollow’);
      function text_rel_nofollow($content)
      return stripslashes(wp_rel_nofollow($content));