1

There we go with my first question ever on WPSE. Hope someone can lend me a hand here :)

I am sick of adding nofollow rel attribute manually on every single outbount link. I´m looking for a code snippet to make external links going from my blog posts into a specific set of external domains nofollow.

This is tricky because I don´t want all external links to become nofollow. Just the ones under specific domains (there´s like 3 domains I want to nofollow).

Any ideas ?

P.S.: Someone had already replied to a similar question How to set all external links to a certain domain to "nofollow"? - I like JMau answer (marked as best answer) except I´m not sure how to insert multiple domain names.

1 Answer 1

0

Instead of the linked option you could just go with something using jQuery. Try adding something like this to your primary theme or plugin javascript file. (Usually something like main.js.)

jQuery( document ).ready( function($) {
    $( 'a[href^="https://google.com"], a[href^="https://yahoo.com"]' ).each( function() {
        $( this ).attr( 'rel', 'nofollow' );
    } );
} );

For any additional URLs you'd simply keep adding a[href^="https://domain.com"] in a comma separated format into the selector.

8
  • Thank you Tony. From a performance and site speed perspective etc., would you say jQuery is better, PHP, or not relevant really? Apr 11, 2020 at 11:40
  • I don’t really think it’s a big deal either way and I guess it really depends on how your site performs right now. JS/jQuery is client side, so it’s actually the users browser executing these changes after they’re already seeing the page. For something this simple I’d usually tend to go with a script. Apr 11, 2020 at 16:26
  • I actually think the answer you linked to is probably a worse idea because it forces the server to execute the process every page load, which, if there's a larger number of links could delay execution of other functions. With the jQuery the server has done it's job and as the user is already seeing elements on the screen the jQuery is doing it's thing. Apr 11, 2020 at 19:10
  • This makes sense. Thank you very mucho Tony for your guidance. I´ll try the script and see how it goes. Apr 13, 2020 at 12:42
  • Just tested this one Tony and it doesn´t seem to be working for me :S Apr 13, 2020 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.