How to add rel=”nofollow” attribute to all links with a specific class in WordPress

This code enables you to dynamically add nofollow to all a href links which contain a specific class. In this case, nofollow is added to all links which contain a class named download.

Once installed, the code will change the HTML output of your links from this :

<a class="download" href="#">Download</a>

To this :

<a class="download" href="#" rel="nofollow">Download</a>

As seen in the following demo video.

Demo Video

Shows nofollow added to a link which includes a specific CSS class.

Tested using the Genesis Sample child theme by StudioPress however will work in any WordPress theme without needing modification.

Installation Steps

There’s 2 steps :

  1. # Copy & paste the PHP code from the functions.php file to the end of your WordPress child themes functions.php file or custom functionality plugin.
  2.  

  3. # Upload the file named nofollow.js to your child theme folder.
  4.  

You can then swap out the classes in the jQuery to match the link classes you want to apply no follow too.

Download Folder

2 responses to “How to add rel=”nofollow” attribute to all links with a specific class in WordPress”

  1. Rachel Robbins Avatar
    Rachel Robbins

    The download appears to contain the incorrect JavaScript file. The PHP file calls for “nofollow.js,” but the included JS file is named “hero.js” and looks like it changes CSS when scrolling.

    Can you advise?

    Thank you!

    1. Sorry about that Rachel. I have too much code!

      Updated https://wpsites.net/download/110861/?tmstv=1692384397

Was This Tutorial Helpful?

Free

$0

Access only to all free tutorials per month.



Monthly

$75

Access to 10 premium tutorials per month.


Tutorial Request


Includes code guarantee and coding support.

Yearly

$500

Access to 15 premium tutorials per month.


Monthly Tutorial Request


Includes code guarantee and priority coding support.