Question

I am searching for a code or better a wordpress plugin to set automatically all links only to a certain domain (and of course to all of its subpages) to "nofollow".

I could not find any plugin which is doing that, maybe you are knowing one or a coding-solution (javascript I guess).

F.e. the plugins I found and their problems:

https://wordpress.org/plugins/nofollow/ Has only "A nofollow option for individual blogroll links" -> so no possibility to set only a certain domain to nofollow which is part of normal articles

https://wordpress.org/plugins/nofollow-links/ Again only general nofollow to domains in the blogroll (or normal links in the articles too? In my understanding a blogroll is a linklist on the sidebar)

wordpress.org/plugins/nofollow-all-external-links Can only give nofollow to all external links, not to a certain domain only

And so on. Anybody with a plugin which allows me to set only a certain domain with its subpages to nofollow?

Was it helpful?

Solution

Assuming you are targetting links in post content, I'd say something like that :

add_filter( 'the_content', 'wpse_254317_nofollow' );
/**
 * Add rel nofollow according to a predefined domain
 * to links in content
 * @param $content
 * @link http://stackoverflow.com/a/4809181/1930236
 * @return mixed
 */
function wpse_254317_nofollow( $content ) {

    $my_folder = "https://google.com";
    preg_match_all( '~<a.*>~isU', $content, $matches );

    for ( $i = 0; $i <= sizeof( $matches[0] ); $i ++ ) {
        if ( isset( $matches[0][ $i ] ) && ! preg_match( '~nofollow~is', $matches[0][ $i ] )
             && ( preg_match( '~' . preg_quote( $my_folder ) . '~', $matches[0][ $i ] )
                  || ! preg_match( '~' . get_bloginfo( 'url' ) . '~', $matches[0][ $i ] ) )
        ) {
            $result = trim( $matches[0][ $i ], ">" );
            $result .= ' rel="nofollow">';
            $content = str_replace( $matches[0][ $i ], $result, $content );
        }
    }

    return $content;
}

The code matches all the links in post content and add the wanted rel for the domain set as $my_folder which is "https://google.com" in my example.

OTHER TIPS

Thank you! I am surprised how many solutions do exist. Another user from here wrote the following code (which include to open each external link automatically, too):

Put this code in your theme functions.php file:

function cdx_handel_external_links() {
    ?>
<script type="text/javascript">
( function( $ ) {

    $("a[href^=http]").click(function(){
      if(this.href.indexOf(location.hostname) == -1) {
         $(this).attr({
            target: "_blank"
         });
      }
    })

   //Add Nofollow
   $("a").each(function(){
    if(this.href.indexOf('EXAMPLEDOMAIN.com') >0 ){
        $(this).attr({
            rel: "nofollow"
         });
    }
   });

} )( jQuery );
</script>
   <?php
}
add_filter( 'wp_footer', 'cdx_handel_external_links', 999);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top