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?

  • Welcome to WPSE. Unfortunately, we don't do plugin recommendations, as we concentrate on development, ie building stuff yourself. What you want is not that difficult to achieve.
    – cjbj
    Jan 28, 2017 at 14:55
  • Ok! Sry for that. But I was asking also for a coding solution without using a plugin. Anybody with such a solution? Jan 29, 2017 at 13:20

2 Answers 2


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.


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( $ ) {

      if(this.href.indexOf(location.hostname) == -1) {
            target: "_blank"

   //Add Nofollow
    if(this.href.indexOf('EXAMPLEDOMAIN.com') >0 ){
            rel: "nofollow"

} )( jQuery );
add_filter( 'wp_footer', 'cdx_handel_external_links', 999);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.