Recently I’ve had an issue on one site where the client requested all internal links on a site to be marked as dofollow. Most links within posts, pages and within sidebar plugins were fine, however problems arose when using the related posts plugin. We have been using the Jetpack Related Posts plugins mainly due to the speed of the plugin and it doesn’t load the database with data. It looks clean out of the box, it has a few options, and is customisable should you wish to change the CSS.
However, one problem I spotted was that there was no easy way of changing the links from nofollow to dofollow. I did some research and found it appeared to be a conscious decision for Jetpack on WordPress.com, and nobody really had a solution on how to do it.
Thankfully, after spending a bit of time, I found a way to make the links dofollow, using a bit of jQuery.
This is done using the jQuery document call ajaxComplete
. This function allows functions to run after all other functions on the site is finished. Unlike the ready
document call, this runs after all Ajax calls are completed. As Jetpack Related Posts uses these calls, we need to run this after the related posts are loaded and displayed on the screen, or else we will effectively remove the nofollow argument from nothing on the page.
Next we need to identify the links we need to remove the nofollow attribute from. Thankfully, Jetpack’s links all have the class jp-relatedposts-post-a
, which allows us to easily target them using the attr
jQuery function.
Finally, the attr
jQuery function simply allows you to get any attribute within the same class or ID. This is useful to grab any attribute and we are setting rel
attribute with the dofollow
attribute.
The reason we are setting it as being dofollow
, rather than just deleting nofollow
, is to keep clean code, or at least code from my standpoint. I don’t like having rel=""
in a link, so that’s personal preference. You can set it to be blank, it’s up to you!
Code To Remove The Make Jetpack Related Posts Nofollow Attribute
In any rate, to remove the nofollow attribute from Jetpack’s related posts’ links, create a new Javascript file called “remove-nofollow-jetpack.js” within a theme or in it’s own plugin, and add the following code:-
1 2 3 | jQuery( document ).ajaxComplete(function() { jQuery("a.jp-relatedposts-post-a").attr("rel", "dofollow"); }); |
You then need to add a call to this script, so in your plugin’s main PHP file, or your theme’s functions.php PHP file, you need to add the following:-
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Enqueue the script that removes nofollow from jetpack * * @link https://www.winwar.co.uk/2015/03/remove-jetpack-related-posts-nofollow-attribute/?utm_source=codesnippet * * @return void */ function winwar_enqueue_remove_nofollow_jetpack_script() { wp_enqueue_script( 'remove-nofollow-jetpack', get_stylesheet_directory_uri() . '/remove-nofollow-jetpack.js', array('jetpack_related-posts'), '1.0' ); } add_action( 'wp_enqueue_scripts', 'winwar_enqueue_remove_nofollow_jetpack_script' ); |
*This code above is used for themes.
This javascript file is also called after the jetpack_related-posts javascript too, to make sure again it loads after the related posts javascript file.
If you wish to test this, the best way I find is to use a Google Chrome plugin called NoFollow, which puts a red dotted border around all nofollowed links. Install this, and load your site again. If the above code works, then you will see no border around the Jetpack Related Posts links.
Anyway, try it yourself, and hope you find it useful!
Hi there and thanks for the post! I’m trying to follow the steps you lined out and have a quick question.
I would put exactly the following in my theme php file? (The reason I ask is that it seems like it’d be calling sylesheets on your site? I have not idea…I’m in over my head right now.)
”
function winwar_enqueue_stylesheets() {
wp_enqueue_script( ‘remove-nofollow-jetpack’, get_stylesheet_directory_uri() . ‘/remove-nofollow-jetpack.js’, array(‘jetpack_related-posts’), ‘1.0’ );
}
add_action( ‘wp_enqueue_scripts’, ‘winwar_enqueue_stylesheets’ );
”
Also, do I have to place the code below the javascript for related posts in order to ensure it works or does the code automatically do that for me?
I’m sure these are stupid question but like I said I’m lost. I really appreciate the help.
April 23, 2015 at 6:34 amHi Spencer :).
Don’t worry, I didn’t make it clear, so I’ve clarified it.
You put the second code block in your theme’s functions.php file. Be careful when copying and pasting it though, as your theme’s functions.php file, if there is an error, causes your site to break, so please make a back up of that file before you begin editing it.
For the javascript file, you need to create a new javascript file. You can do this in any text editor, copy that code to the text file and then save it as “remove-nofollow-jetpack.js”. Make sure the file is saved in the same folder as your theme’s functions.php file for it to work.
The “stylesheet” referred to is a WordPress function called “get_stylesheet_directory_uri()”. This gets the directory the stylesheet of the theme is in, which is the same directory as the functions.php file (and the file remove-nofollow-jetpack.js file is in).
Hope that helps!
Rhys
April 23, 2015 at 7:39 amThanks a ton for the quick response!
I do have one more quick question. I noticed after I wrote my comment that the related posts here are actually nofollow. Is there a reason you decided to turn the related posts plugin back to default? I ask b/c I’m a little confused as to why they would nofollow those links in the first place – in fact, it makes zero sense to me – so I want to make sure I’m not overlooking something.
Thanks again.
April 23, 2015 at 7:46 amI didn’t implement it on this site as I couldn’t be bothered if I’m honest. Client asked for it, and I added it. I have no issue with those links being no followed here.
I cannot speak for Jetpack as to why they nofollow the related posts. If you ask that question in their specific WordPress forum, they may get back to you.
April 23, 2015 at 7:49 amvery good solution… now i implement it in my site..
May 21, 2015 at 8:26 am