Support » Fixing WordPress » Replacing nofollow rel from cloaked link

  • dear all,
    i need your support about the replace of nofollow rel attribute.

    I got a lot of internal links “/link/pluto” (cloaked) with nofollow attribute. I need to remove the nofollow attribute only on this links from all my website.

    Many of this got the sitename before and other not only the partial url /link/pppp

    I need a function to insert inside child theme to solve my issue.

    Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    It depends on where the links are coming from. If they are in post content, you would add a callback to the “the_content” filter. The code goes in functions.php. The linked page has an example search/replace code using preg_replace() near the bottom that you could adapt to your needs.

    ok i see but i don’t know to do it. Thanks.

    Moderator bcworkz

    (@bcworkz)

    Try this (untested):

    add_filter( 'the_content', 'my_string_replacements');
    function my_string_replacements ( $content ) {
        if ( ! is_admin() && is_main_query() ) {
            return preg_replace('/<a(.*href=".*\/link\/pluto\/?".*)>/', '<a$1 rel="nofollow">', $content );
        }
        return $content;
    }

    It’ll add the nofollow attribute to any link that contains /link/pluto in the URL. Anything before that string, like a domain name, will still match. Other than a trailing /, anything in the URL after that string will fail to match. If there is a link that already has the nofollow attribute, this will still add it again.

    Hi thanks for your support
    tried but it doesn’t work. We can extend it only to all url with /link/ inside. Nofollow isn’t alone. It could be with noopener.
    Thanks again.
    If you solve my issue, you’ll be my hero.

    • This reply was modified 2 months, 3 weeks ago by gennymac.

    Hi,
    i solved my issue with this code:

    add_filter( 'the_content', 'ex1_the_content_filter' );
    function ex1_the_content_filter($content) {
    
    // finds all links in your content with a nofollow
    preg_match_all('/\<a .*?nofollow.*?a>/',$content,$matches, PREG_SET_ORDER);
    
    // loop through all matches
    foreach($matches as $m){
        // potential link to be replaced...
        $toReplace = $m[0];
    
        // You can add whatever additional "IF" conditions you require to this one
        if (preg_match('/.*?\/link.*?/',$toReplace)){
            // removes rel="nofollow" from the current link
            $replacement = preg_replace('/(<a.*?)(nofollow )(.*?a\>)/','$1$3',$toReplace);
            // replaces the current link with the $replacement string
            $content = str_ireplace($toReplace,$replacement,$content);
        }
     }
      return $content;
    }

    Thanks for you support. You were precious.

    • This reply was modified 2 months, 3 weeks ago by gennymac.
    Moderator bcworkz

    (@bcworkz)

    Awesome! You’re welcome.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.