How to Add rel=”nofollow” to WordPress Navigation Menu Links

Posted by on Jan 10, 2014 in WordPress ·

WordPress by default sets all the navigation menu links DoFollow. It simply doesn’t add any relation (rel=” “) to those menu links. But many people still don’t know that they can make the menu links NoFollow very easily. Let’s see how to make the links in the navigation menu NoFollow.

Add Nofollow to WordPress Navigation Menu Links

Why would I need to make the menu links NoFollow?

Internal as well as external link structure of every website is very important for SEO. For that, making the links NoFollow may required. Also, if you have signup and signin links in the menu, you might want to make the links NoFollow to prioritize other links. If you are linking to external website/s using menu, you might need not to pass the link juice to that site/s. So, there are number of reasons why you would need to make the menu links NoFollow.

• Related : The difference between DoFollow and NoFollow hyper links.

How to make the navigation menu links NoFollow?

There are two ways to do this. One is very simple and the other one involves coding.

Method 1 : WordPress Menu Settings

This method is applicable for users having WordPress version 3.0 or later. Make sure that you have the latest version of WordPress.

Step 1 : Login to WordPress admin. Then go to Appearance -> Menus -> Edit Menus tab. You will see all the menus you have created. Select your menu to edit it.

Step 2 : On the upper right corner, you will see the screen options. Click on that button to expand all the screen options available.

Locating screen options in menu settings

Locating screen options in menu settings

Step 3 : Check Link Relationship (XFN) to enable rel attribute for menu links.

Enable Link Relationship in Screen Options

Enable Link Relationship in Screen Options

Step 4 : Now, you will see a new text-box named “Link Relationship (XFN)” under each menu link. Set that value as “nofollow” for the links you want.

Set the NoFollow link relation

Set the NoFollow link relation

Click on Save Menu button to save the menu items.

Done! If you open your homepage and check its source, you will find rel=”nofollow” in the menu links you set nofollow.

Method 2 : Custom coding

The drawback of the above method is that you cannot control when to make the links nofollow. If you don’t want the links nofollowed everywhere, just for particular menu locations, here is the code. Add it to your theme’s functions.php file.

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

Code was originally posted on : Katz.co.

Modifications to the code :

Line #3 : $nofollow = array(105,268);

This is an array of comma separated ID of menu items. To get the ID of an item, view the source and check the ID. Please refer the image below.

Identifying the ID of menu item

Identifying the ID of menu item

Line #4 : $location = '';

This the location where you want to make the links NoFollow. To find the location, go to Appearance -> Menus -> Manage Locations tab. You will find all the locations available in your theme.

Locations for Menus

Locations for Menus

Set the value of $location variable as exactly the name of the location. Notice that, this is not an array. You can set only one location value. For example, $location = 'Header';.

Line #5 : $menu = '';

If you want to filter by the menu name, set the value of this variable as exactly the menu name. The menu name is the value you gave to it while creating it. In the above image, my menu name is “Menu”. So, I will make that line as – $location = 'Menu';.

So, this was how to make navigation menu links NoFollow. Have you implemented any of these methods on your WordPress blog?

15 Comments on “How to Add rel=”nofollow” to WordPress Navigation Menu Links”

  1. interesting! Was just debating this with some silo-junkies. 😉
    What’s your recommendation for the top-level navmenu?
    Nofollow or….?

  2. Hi Ani, been looking for a solution for a while, so cheers for this. Been using plugins and all sorts, and now I find it’s already in WordpRess screen options! Cheers,

    James

  3. Hi,
    I’ve been trying to get the function to work on the qtranslate-x language switcher in a menu, but no luck. Any ideas?

Leave a Reply