Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top