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;
}

Author: Zack Katz

Zack Katz is the President of Katz Web Services and the developer of WordPress plugins with over 700,000 downloads. He lives in Southwest Colorado with his wife and two cats.

24 thoughts on “Two Easy Ways to Add “nofollow” to WordPress Menu Items”

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

  2. 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.  🙂

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

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

    1. 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 ) {
      foreach ($items as $item) {
      $item->xfn = ‘nofollow’;
      }
      return $items;
      }
      add_filter(‘widget_text’, ‘text_rel_nofollow’);
      function text_rel_nofollow($content) {
      return stripslashes(wp_rel_nofollow($content));
      }

Leave a Reply

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