Skip to content

Mstoic

  • Home
  • Start Here!
  • Best Deals
  • Facebook Tricks
  • Privacy
  • Resources
  • TOC
  • Tools
    • Web Tools
      • Stop Watch
      • QR Code Generator
      • Adsense Code Converter
      • Blogger Sitemap Generator
      • Difference Between Two Texts
      • Vacation Checklist
      • Twitter Symbols
      • Count Down
    • Investment
      • SIP Calculator
      • Power of Compounding
      • PPF Calculator
      • FD Calculator
  • About Us

No-follow All External Links In WordPress Without Plugin

Editorial Team | July 7, 2014January 14, 2017 | Webmasters, Wordpress |

With Google getting more and more concerned about quality, it has become very difficult to maintain your rank in search results. The first thing to rank higher in SERPs is to have good quality content on your website. One more thing Google takes into account while ranking web pages is the Page Rank, introduced by Google itself. But Page Rank is volatile, it passes on to the web pages that a page links to. And hence you must be very careful while linking to web pages outside your domain.

Page rank has one more quality, it is not passed to links that are no-followed, and that is why we have a code snippet that can automatically no-follow all the links to external websites.

add_filter('the_content', 'my_nofollow');
add_filter('the_excerpt', 'my_nofollow');

function my_nofollow($content) {
    return preg_replace_callback('/]+/', '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;
}

This code no-follows all the external links from the_content and the_excerpt functions. Just place this code in the functions.php file of your active theme and it takes care of everything else. If you are a newbie and have never edited the functions.php file, then this guide can help.

Previous Post

Set Custom Gravatar Image As Default In WordPress

by Editorial Team

Next Post

Customizing The Tag Cloud In WordPress

by Editorial Team

Recommended Next
Display Blog Subscribers In WordPress

Discussions

  1. ARM
    July 7, 2014 at 15:36

    I never knew we could do this without a plugin. Thanks for your tutorial.

    Reply

Your Comment Cancel reply

Follow Us

Like us Follow us Follow us

Categories

  • Tricks (93)
  • Facebook (83)
  • Tips (76)
  • Windows (67)
  • Wordpress (39)
  • Android (38)
  • Internet (33)
  • Antivirus (30)
  • How To (29)
  • Customization (25)
  • Webmasters (25)
  • Downloads (21)
  • Utilities (21)
  • Gmail (20)
  • Windows 7 (19)

Related Articles

  • Reverse a String using StringBuilder