Categories
Tutorial WordPress

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

By Zack Katz

Zack Katz is the founder of GravityKit and TrustedLogin. He lives in Leverett, Massachusetts with his wife Juniper.

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

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.

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

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

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

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.

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

Comments are closed.