• Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    Monitoring Web Page Changes with Python

    Monitoring Web Page Changes with Python

    My SSH Setup: How I Manage Multiple Servers

    My SSH Setup: How I Manage Multiple Servers

    Building a Network Tracker Auditor for Privacy with Python

    Building a Network Tracker Auditor for Privacy with Python

    Streaming Audio Files Securely with PHP

    Streaming Audio Files Securely with PHP

    Scraping Web Data with Python Helium

    Scraping Web Data with Python Helium

    Building a Secure 2FA Authenticator with Python

    Building a Secure 2FA Authenticator with Python

    Building a Cache Warmer with Python

    Building a Cache Warmer with Python

    How to Create a Python GUI to Launch Webhooks

    How to Create a Python GUI to Launch Webhooks

    Mastering python-docx A Guide to Creating Word Documents with Python

    Mastering python-docx: A Guide to Creating Word Documents with Python

  • Blog
    • All
    • Artificial Intelligence
    • Privacy
    • Reviews
    • Security
    • Tutorials
    AdGuard Ad Blocker Review

    AdGuard Ad Blocker Review

    Surfshark VPN Review

    Surfshark VPN Review

    Nmap Unleash the Power of Cybersecurity Scanning

    Nmap: Unleash the Power of Cybersecurity Scanning

    Floorp Browser Review

    Floorp Browser Review

    Understanding Man-in-the-Middle Attacks

    Understanding Man-in-the-Middle Attacks

    Privacy-Focused Analytics

    Privacy-Focused Analytics: Balancing Insights and Integrity

    Safeguarding Your Facebook Account

    Safeguarding Your Facebook Account: Understanding the Differences Between Hacking and Cloning

    38 essential points to harden WordPress

    38 Essential Points to Harden WordPress

    10 Tips and Tricks to Secure Your WordPress Website

    10 Tips and Tricks to Securing Your WordPress Website

  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact
    • General Contact
    • Website Technical Support
No Result
View All Result
  • Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    Monitoring Web Page Changes with Python

    Monitoring Web Page Changes with Python

    My SSH Setup: How I Manage Multiple Servers

    My SSH Setup: How I Manage Multiple Servers

    Building a Network Tracker Auditor for Privacy with Python

    Building a Network Tracker Auditor for Privacy with Python

    Streaming Audio Files Securely with PHP

    Streaming Audio Files Securely with PHP

    Scraping Web Data with Python Helium

    Scraping Web Data with Python Helium

    Building a Secure 2FA Authenticator with Python

    Building a Secure 2FA Authenticator with Python

    Building a Cache Warmer with Python

    Building a Cache Warmer with Python

    How to Create a Python GUI to Launch Webhooks

    How to Create a Python GUI to Launch Webhooks

    Mastering python-docx A Guide to Creating Word Documents with Python

    Mastering python-docx: A Guide to Creating Word Documents with Python

  • Blog
    • All
    • Artificial Intelligence
    • Privacy
    • Reviews
    • Security
    • Tutorials
    AdGuard Ad Blocker Review

    AdGuard Ad Blocker Review

    Surfshark VPN Review

    Surfshark VPN Review

    Nmap Unleash the Power of Cybersecurity Scanning

    Nmap: Unleash the Power of Cybersecurity Scanning

    Floorp Browser Review

    Floorp Browser Review

    Understanding Man-in-the-Middle Attacks

    Understanding Man-in-the-Middle Attacks

    Privacy-Focused Analytics

    Privacy-Focused Analytics: Balancing Insights and Integrity

    Safeguarding Your Facebook Account

    Safeguarding Your Facebook Account: Understanding the Differences Between Hacking and Cloning

    38 essential points to harden WordPress

    38 Essential Points to Harden WordPress

    10 Tips and Tricks to Secure Your WordPress Website

    10 Tips and Tricks to Securing Your WordPress Website

  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact
    • General Contact
    • Website Technical Support
No Result
View All Result
Home Guides WordPress

Add NoFollow to External Links in WordPress

Jonathan Moore by Jonathan Moore
4 years ago
Reading Time: 4 mins read
A A
Add NoFollow to External Links in WordPress
FacebookTwitter

Links are commonly used within posts on WordPress to point the user to an external location that may be of interest to them. Sometimes an external link may point to a questionable website or one with no reputable value. When we link to an external website, search engines pass a small part of link authority from your website to the linked website. We don’t want to give authority to all websites that we may link to because it may hurt our search ranking later on down the road. To avoid this issue, we can add the link attribute rel=”nofollow” to the link.

<a href="https://example.com" rel="nofollow">Visit Example Website</a>

That is easy enough, but what if you have a lot of external links that you need to add the nofollow attribute to? That is where this handy little WordPress snippet comes into play. It will automate the process of adding the attribute to all external links. I have also taken the time to add multiple options within the snippet that you can use depending on how you want to handle the links.

The snippet will also detect if there are currently any link rel attributes added already and instead of removing them, it will add the nofollow attribute to them.

You can add this snippet to your child theme’s functions.php file, but I recommend installing a really cool plugin called Code Snippets. Code Snippets is an easy, clean and simple way to run code snippets on your site. It removes the need to add custom snippets to your theme’s functions.php file by providing a GUI interface in your Admin Dashboard.

function strposa( $haystack, $needle, $offset=0 ) {
    if ( !is_array( $needle ) )
        $needle = array($needle);
    foreach ( $needle as $query ) {
        if ( strpos( $haystack, $query, $offset ) !== false )
            return true;
    }
    return false;
}

function domain_in_whitelist( $content, $domains ) {
    $doc = new DOMDocument();
    $doc->loadHTML( $content );
    $xpath = new DOMXPath( $doc );
    $nodeList = $xpath->query( '//a/@href' );
    for ( $i = 0; $i < $nodeList->length; $i++ ) {
        $url = parse_url( $nodeList->item( $i )->value )['host'];
        if ( strposa( $url, $domains ) ) {
            return true;
        }       
    }
}

function add_nofollow_callback( $content, $whitelist_domains="" ) {
    return preg_replace_callback('|<a (.+?)>|i', function( $matches ) use ( $whitelist_domains ) {
        $text = $matches[1];
        $site_link = get_bloginfo( 'url' );

        if ( strpos( $text, $site_link ) || domain_in_whitelist( $matches[0], $whitelist_domains ) ) {
            return "<a $text>";
        }

        if ( strpos( $text, "rel=" ) ) {
            if ( strpos( $text, "nofollow" ) === false ) {
                $text = preg_replace( '/rel="(.*)"/isU', 'rel="$1 nofollow"', $text );
                return "<a $text>";
            } else {
                return "<a $text>";
            }
        } else {
            if ( strpos( $text, "nofollow" ) === false ) {
                $text = preg_replace( '/href=(.*)/i', 'href=$1 rel="nofollow"', $text );
                return "<a $text>";
            } else {
                return "<a $text>";
            }
        }
        return "<a $text>";
    }, $content);
}

function add_rel_nofollow( $content ) {
    /*
        Uncomment the code by removing the "//" in front of each option that you would 
        like to use. If you would like to whitelist certain domains from being modified 
        to use the rel nofollow attribution, you can add the domains to the
        $whitelist_domains array below.
    */
    
    // Add Domains to be Whitelisted
    $whitelist_domains = array(
        'example.com',
        'example3.com'
    );
  
    // Whitelist Single Post
    // if ( is_single( '' ) ) {
    //     return $content;
    // }
    
    // Whitelist Multiple Posts
    // if ( is_single( array( '', '' ) ) ) {
    //     return $content;
    // }
    
    // Whitelist Single Category
    // if ( in_category( 'category-slug' ) ) {
    //    return $content;
    // }
    
    // Whitelist Multiple Categories
    // if ( in_category( array( 'category-slug', 'category2-slug' ) ) ) {
    //     return $content;
    // }    
    
    // Add NoFollow to Single Post except for Domains Whitelisted
    // if ( is_single( '36007' ) ) {
    //     return add_nofollow_callback( $content, $whitelist_domains );
    // } else {
    //     return $content;
    // }
    
    // Add NoFollow to Multiple Posts except for Domains Whitelisted
    // if ( is_single( array( '36007', '35334' ) ) ) {
    //     return add_nofollow_callback( $content, $whitelist_domains );
    // } else {
    //     return $content;
    // }
    
    // Add NoFollow to Single Category except for Domains Whitelisted
    // if ( in_category( 'category-slug' ) ) {
    //     return add_nofollow_callback( $content, $whitelist_domains );
    // } else {
    //     return $content;
    // }
    
    // Add NoFollow to Multiple Categories except for Domains Whitelisted
    // if ( in_category( array( 'category-slug', 'category2-slug' ) ) ) {
    //     return add_nofollow_callback( $content, $whitelist_domains );
    // } else {
    //     return $content;
    // }  
    
    // Add to All External Links except for Domains Whitelisted
    return add_nofollow_callback( $content, $whitelist_domains );
}
add_filter( 'the_content', 'add_rel_nofollow' );

 

Test Content

<a href="https://example.com">example.com</a>
<a href="https://www.example.com">www.example.com</a>
<a href="https://www.example2.com">www.example2.com</a>
<a href="https://test.example3.com">test.example3.com</a>
<a href="https://www.example4.com">www.example4.com</a>
<a href="https://www.example5.com">www.example5.com</a>

Output

<a href="https://example.com">example.com</a>
<a href="https://www.example.com">www.example.com</a>
<a href="https://www.example2.com" rel="nofollow">www.example2.com</a>
<a href="https://test.example3.com">test.example3.com</a>
<a href="https://www.example4.com" rel="nofollow">www.example4.com</a>
<a href="https://www.example5.com" rel="nofollow">www.example5.com</a>

 

Tags: PHPSEOWordPress
ShareTweetSharePinShareShareScan
ADVERTISEMENT
Jonathan Moore

Jonathan Moore

Senior Software Engineer and Cybersecurity Specialist with over 3 decades of experience in developing web, desktop, and server applications for Linux and Windows-based operating systems. Worked on numerous projects, including automation, artificial intelligence, data analysis, application programming interfaces, intrusion detection systems, streaming audio servers, WordPress plugins, and much more.

Related Articles

Streaming Audio Files Securely with PHP

Streaming Audio Files Securely with PHP

PHP is widely used for managing file access, handling data securely, and serving multimedia content online. One of the common...

Groundhog Day: Predicting Early Spring with PHP

Groundhog Day: Predicting Early Spring with PHP

In the quaint town of Punxsutawney, Pennsylvania, a unique tradition unfolds each year on February 2nd, known as Groundhog Day....

Automating JavaScript Minification with PHP

Automating JavaScript Minification with PHP

Automating JavaScript minification with PHP can significantly streamline a developer's workflow. It ensures that the deployed code is always in...

Next Post
Retrieve Weather Data with Python

Retrieve Weather Data with Python

Recommended Services

Latest Articles

Monitoring Web Page Changes with Python

Monitoring Web Page Changes with Python

There are times when I need to know that a web page has changed without actively watching it. That might...

Read moreDetails

My SSH Setup: How I Manage Multiple Servers

My SSH Setup: How I Manage Multiple Servers

If you work with more than one server, the need to manage multiple servers with SSH becomes obvious pretty quickly....

Read moreDetails

Building a Network Tracker Auditor for Privacy with Python

Building a Network Tracker Auditor for Privacy with Python

In my last post, I dug into AdGuard, a robust ad blocker that tackles trackers and ads head-on. But how...

Read moreDetails

AdGuard Ad Blocker Review

AdGuard Ad Blocker Review

Ad blocking software has become essential for anyone who values a clean, fast, and secure browsing experience. With the ever-increasing...

Read moreDetails
  • Privacy Policy
  • Terms of Service

© 2025 JMooreWV. All rights reserved.

No Result
View All Result
  • Home
  • Guides
    • Linux
    • Programming
      • JavaScript
      • PHP
      • Python
    • Tools
    • WordPress
  • Blog
    • Artificial Intelligence
    • Tutorials
    • Privacy
    • Security
  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact
    • General Contact
    • Website Technical Support