• Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    My Backup Setup for Linux PCs

    My Backup Setup for Linux PCs

    Detecting Hidden WordPress Malware Disguised as Images

    Detecting Hidden WordPress Malware Disguised as Images

    Server-Side Image Conversion with Apache

    Server-Side Image Conversion with Apache

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Monitor SSL Expiration with Python

    Monitor SSL Expiration with Python

    Building a Simple WordPress Post List Tool with PHP

    Building a Simple WordPress Post List Tool with PHP

    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

  • Blog
    • All
    • Artificial Intelligence
    • Developer Life
    • Privacy
    • Reviews
    • Security
    • Tutorials
    Imposter Syndrome as a Self-Taught Developer

    Imposter Syndrome as a Self-Taught Developer

    Why Stable Websites Outperform Flashy Redesigns

    Why Stable Websites Outperform Flashy Redesigns

    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

  • 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 Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity
No Result
View All Result
  • Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    My Backup Setup for Linux PCs

    My Backup Setup for Linux PCs

    Detecting Hidden WordPress Malware Disguised as Images

    Detecting Hidden WordPress Malware Disguised as Images

    Server-Side Image Conversion with Apache

    Server-Side Image Conversion with Apache

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Monitor SSL Expiration with Python

    Monitor SSL Expiration with Python

    Building a Simple WordPress Post List Tool with PHP

    Building a Simple WordPress Post List Tool with PHP

    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

  • Blog
    • All
    • Artificial Intelligence
    • Developer Life
    • Privacy
    • Reviews
    • Security
    • Tutorials
    Imposter Syndrome as a Self-Taught Developer

    Imposter Syndrome as a Self-Taught Developer

    Why Stable Websites Outperform Flashy Redesigns

    Why Stable Websites Outperform Flashy Redesigns

    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

  • 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 Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity
No Result
View All Result
Home Guides WordPress

Add NoFollow to External Links in WordPress

Jonathan Moore by Jonathan Moore
5 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

I am a Software Architect and Senior Software Engineer with 30+ years of experience building applications for Linux and Windows systems. I focus on system architecture, custom web platforms, server infrastructure, and security-focused tools, with an emphasis on performance and reliability. Over the years, I have built everything from WordPress plugins and automation systems to full platforms, ad serving systems, monitoring tools, and API-driven applications. I prefer working close to the system, solving real problems, and building tools that are meant to be used.

Related Articles

Detecting Hidden WordPress Malware Disguised as Images

Detecting Hidden WordPress Malware Disguised as Images

With the recent discovery of malicious files like Stained_Heart_Red-600x500.png being found on servers across the web, it pushed me to...

Server-Side Image Conversion with Apache

Server-Side Image Conversion with Apache

I stopped relying on third party image services a while ago. They work, but they add cost, latency, and another...

Building a Simple WordPress Post List Tool with PHP

Building a Simple WordPress Post List Tool with PHP

I needed a quick way to view all my WordPress posts without logging into the admin dashboard. Sometimes you just...

Next Post
Retrieve Weather Data with Python

Retrieve Weather Data with Python

Recommended Services

Latest Articles

My Backup Setup for Linux PCs

My Backup Setup for Linux PCs

March 31st is World Backup Day. It is meant to be a reminder, but I do not rely on reminders...

Read moreDetails

Detecting Hidden WordPress Malware Disguised as Images

Detecting Hidden WordPress Malware Disguised as Images

With the recent discovery of malicious files like Stained_Heart_Red-600x500.png being found on servers across the web, it pushed me to...

Read moreDetails

Server-Side Image Conversion with Apache

Server-Side Image Conversion with Apache

I stopped relying on third party image services a while ago. They work, but they add cost, latency, and another...

Read moreDetails

Imposter Syndrome as a Self-Taught Developer

Imposter Syndrome as a Self-Taught Developer

I started writing code over 35 years ago. Everything I learned came from figuring things out on my own, long...

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 Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity