Auto NoFollow External Links

Posted on 26th September, 2015 82 Comments

This snip will add a nofollow attribute to all external links.

function.php
/* 
 * 	NOFOLLOW EXTERNAL LINKS
 *************************************************************/
add_filter('the_content', 'my_nofollow');
add_filter('the_excerpt', 'my_nofollow');
function my_nofollow($content) {
    return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
}
function my_nofollow_callback($matches) {
    $link = $matches[0];
    $site_link = get_bloginfo('url');
    if (strpos($link, 'rel') === false) {
        $link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
    } elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
        $link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
    }
    return $link;
}

Comments

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]

JamesVen

21st, Oct, 20

for an neurogenic linseed filament is danged unobstructed or [url=https://ciamedusa.com/]cialis pills[/url] Lastly can be much geezer

JamesVen

21st, Oct, 20

would be required reassuring, care settings or chemicals on exertion. [url=https://ciamedusa.com/]tadalafil vs sildenafil[/url] Ukcofc eoytgi

Elmo Apland

01st, Nov, 20

I’d need to verify with you here. Which is not one thing I often do! I take pleasure in reading a submit that can make people think. Also, thanks for permitting me to remark!

my explanation 24 hr electrician

03rd, Dec, 20

Great beat ! I wish to apprentice whilst you amend your website, how could i subscribe for a weblog site? The account aided me a acceptable deal. I had been a little bit familiar of this your broadcast provided shiny transparent idea

Life Experience Degrees

07th, Dec, 20

Hi, There’s no doubt that your blog could be having web browser compatibility problems. Whenever I look at your blog in Safari, it looks fine however when opening in I.E., it’s got some overlapping issues. I simply wanted to provide you with a quick heads up! Apart from that, great blog.

Johnnie Conard

04th, Jan, 21

I’m looking for a nice digital camera under or around $200 for the purpose of blogging (as in wordpress, blogger, etc). I’m trying to get a blog which will be based around the daily photos I take despite the fact that I’m not really a photographer but only doing it for fun. Any cool features like video would also be great but mainly I’m just looking for something I can take quality pictures, and that will hopefully hide my total amateur abilities. . . Thanks guys!.

Ernestina Hibler

07th, Jan, 21

I take pleasure in, result in I discovered just what I was looking for. You’ve ended my four day long hunt! God Bless you man. Have a nice day. Bye

hey

07th, Jan, 21

Taxi moto line
128 Rue la Boétie
75008 Paris
+33 6 51 612 712  

Taxi moto paris

You really make it appear so easy along with your presentation however I in finding this matter to be really something which
I think I might by no means understand. It kind of feels too complex and
extremely huge for me. I am having a look forward
for your subsequent publish, I will attempt to get the
hang of it!

vreyrolinomit

10th, Jan, 21

Wow, superb blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is great, as well as the content!

호텔카지노

15th, Jan, 21

Terrific paintings! That is the type of info that are meant to be shared around the internet. Disgrace on Google for no longer positioning this submit higher! Come on over and consult with my site . Thanks =)

더킹카지노

22nd, Jan, 21

Some genuinely nice and useful information on this web site, besides I think the style contains excellent features.

Google

23rd, Jan, 21

Google

That will be the finish of this article. Right here you will uncover some websites that we think you’ll value, just click the hyperlinks.

zortilo nrel

26th, Jan, 21

I keep listening to the newscast speak about getting boundless online grant applications so I have been looking around for the most excellent site to get one. Could you tell me please, where could i acquire some?

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]