Question

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

Was it helpful?

Solution

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;
}

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top