Add rel = "nofollow" and target = "_ blank" for external links permanently

I would like to add rel = "nofollow" and target = "_blank" to all external links in my posts and Wordpress pages forever. I know there are plugins out there that do the same, but once they are disabled, all changes will be reverted and the articles will be the same as from the beginning.

I don't know how to distinguish between internal or external links and how to check if there is a rel = "nofollow" or target = "_blank" attribute.

I think the best way to do this is to use PHP instead of MySQL. I have already searched the web pages for tutorials, tutorials or plugins with no success.

Can anyone help me? I appreciate your support.

+3


source to share


4 answers


I have a solution for applying nofollow to all existing and new xrefs. Copy the code to your functions. Php activated theme

function add_nofollow_content($content) {
$content = preg_replace_callback('/]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i', function($m) {
    if (strpos($m[1], "YOUR_DOMAIN_ADDRESS") === false)
        return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
    else
        return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
    }, $content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');

      



You can also call the function home_url()

instead "YOUR_DOMAIN_ADDRESS"

in the specified space to avoid hardcoding the domain name.

The code has been tested and it works. Hope it helps.

+5


source


You can use the following snippet: http://wpsnipp.com/index.php/functions-php/nofollow-external-links-only-the_content-and-the_excerpt/

This is a big little snippet that will add rel = "nofollow" to the external link both internally and in fact. Add this snippet to the functions.php of your wordpress theme to enable 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;
}

      

+1


source


I think that the addition rel"nofollow"

and target="_blank"

outbound links constantly - it's more work than is shown here. You will have to rebuild plugin functionality such as External Links so that even the links in yours can be rewritten wp_nav_menus

.

I have a suggestion that adds the required attributes via JavaScript on page load. You can add this script directly to the theme header, or you can save it in a separate file by inserting the script into your themes' functions.php

:

$(document).ready(function () {
    $( "a:not(a[href^='http://www.your-domain-name.com'],a[href^='javascript'],a[href^='#'])" ).attr({
                rel: "nofollow",
                target: "_blank"
            });
});

      

0


source


I took @ rony-samuel's answer and fixed a few things that you might find useful.

Use the built-in function make_clickable

to wrap links automatically. (Useful for example when creating messages via API) - then check if the user added additional classes to the link (for example button

, to have a different style) - we don't want to overwrite this, so we just return the given markup with $m[0]

.

The last thing is a regular expression. In combination with make_clickable

it, it will give a <a <a href...

link in a link. I have corrected the regex to avoid this.

// Auto warp links within content
add_filter( 'the_content', 'make_clickable' );

// Add target blank and nofollow to external links
// Do not overwrite links that probably have been placed manually in the content
// and contain classes like "button" or whatever etc. Since they were placed manually
// with additional styling, the editor can add target="_blank" manually as well if needed.

function external_links ($content) {
    $content = preg_replace_callback(
    '/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
    function($m) {
        $hasClass = (bool) preg_match('/class="[^"]*[^"]*"/', $m[0]);

        if (strpos($m[1], home_url()) === false && $hasClass === false)
            return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
        else
            return $m[0];
        },

        $content);
        return $content;
    }

// set a very low priority to ensure,
// all the content and shortcode things has been completed already
add_filter('the_content', 'external_links', 999);

      

0


source







All Articles