0

I got all my website with a lot of internal cloaked links with a nofollow attribute.

I need a function or filter to add inside functions.php that removes the nofollow attribute when the permalink contains a specific word es "/link/pluto". Could you help me?

Thanks for your support. Gp

0

I wrote the following code to address ADDING tag attributes, but here is a version to help you locate, and remove nofollow:

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('/.*?\/pluto.*?/',$toReplace)){
            // removes rel="nofollow" from the current link
            $replacement = preg_replace('/(<a.*?)(rel="nofollow")(.*?a\>)/','$1$3',$toReplace);
            // replaces the current link with the $replacement string
            $content = str_ireplace($toReplace,$replacement,$content);
        }
    }
      return $content;
}
1

you are my hero. I solved my issue with internal links with your code. I had to modify it because i got many different rel inside my code rel="nofollow nooopener" and rel="nooopener nofollow" so i modify with this:

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;
}
  • Nice adjustment! Glad it worked out for you. – Mike Baxter Aug 27 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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