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)
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; }
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.
I dont now why, but for me not working. I use flexsqueze theme.
I am implementing it on my website now. Thank you
Great tip, thank you.
Thank’s for tips. It’s very helpful. 🙂
woohoo thanks
Helpful tips, thanks to share with us.
Great tute … bro !! Will be helpful in preserving the link juice to pass on to about and contact pages .. Thanks for the share
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. 🙂
Been looking for solution on this thing, thanks for sharing this technique.
Link Relationship option is not available in my wordpress dashboard. What to do?
Did you read Step 1?
Wow! This solved my problem! I never knew what Screen Options was for until I read your post. Thank you!
You’re most welcome 🙂
This attribute is very much useful but is it compatible with other lower versions too?
Great tip! Don’t see why wordpress don’t show this as default 🙂
I think because it would confuse most users who would not understand what it is or how to fill it out.
I also want to know ‘This attribute is very much useful but is it compatible with other lower versions too?’
I believe it’s compatible from 2.8+
It’s a nice tips.thanks for share…. Keep it up man
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));
}
great
thanks!!