0

I'm trying to exclude several urls from appending a nofollow rule using strpos but it only works with 1 url, I can't get it to exclude all the urls I need to include.

This is the code that I've tried:

add_filter( 'custom_bbpress_nofollow', 'my_custom_bbpress_nofollow', 15, 2 );
function my_custom_bbpress_nofollow( $nofollow, $href ) {

if ( strpos( $href, 'https://www.examplelink1.com' ) === false && strpos( $href, 'https://www.examplelink2.com' ) === false && strpos( $href, 'https://www.examplelink3.com' ) === false && strpos( $href, 'https://examplelink4.to' ) === false) {
       $nofollow = '';
   }

   return $nofollow;
}

I've tried it with == false and !== false as well and those don't seem to work.

This one DOES work, but it only excludes a single domain, I need to be able to exclude 5 in all.

add_filter( 'custom_bbpress_nofollow', 'my_custom_bbpress_nofollow', 15, 2 );
function my_custom_bbpress_nofollow( $nofollow, $href ) {

    if ( strpos( $href, 'http://your-domain.here' ) !== false ) {
        $nofollow = 'rel="dofollow"';
    }

    return $nofollow;
}

What is the correct strpos syntax to include multiple urls,

https://www.examplelink1.com AND/OR
https://www.examplelink2.com AND/OR
https://www.examplelink3.com AND/OR
https://www.examplelink4.com
Liss
  • 15
  • 5

2 Answers2

0

I believe you want to use || (OR) instead of && (AND) in your if statement. Your $href will never match ALL conditions, so using AND will never return true. With OR you tell it to set $nofollow = '' if ANY of those URLs is found.

add_filter( 'custom_bbpress_nofollow', 'my_custom_bbpress_nofollow', 15, 2 );
function my_custom_bbpress_nofollow( $nofollow, $href ) {

if ( strpos( $href, 'https://www.examplelink1.com' ) === false || strpos( $href, 'https://www.examplelink2.com' ) === false || strpos( $href, 'https://www.examplelink3.com' ) === false || strpos( $href, 'https://examplelink4.to' ) === false) {
   $nofollow = '';
}

return $nofollow;
}

Edit: Actually, I'm not too sure what you're trying to do. Your 2 examples have opposite code, which is confusing. If you wish to return 'rel="nofollow"' if href equals ANY of the 5 urls, then try this:

 add_filter( 'custom_bbpress_nofollow', 'my_custom_bbpress_nofollow', 15, 2 );

 function my_custom_bbpress_nofollow( $nofollow, $href ) {
     $nofollow = '';
     if ( strpos( $href, 'http://your-domain.here' ) !== false || 
          strpos( $href, 'https://www.examplelink1.com' ) !== false || 
          strpos( $href, 'https://www.examplelink2.com' ) !== false || 
          strpos( $href, 'https://www.examplelink3.com' ) !== false || 
          strpos( $href, 'https://www.examplelink4.com' ) !== false 
    ) {
         $nofollow = 'rel="dofollow"';
     }

    return $nofollow;
 }

You should also define $nofollow at the start of the function, otherwise nothing can be returned if the condition doesn't match.

0

Sorry, I want it to DOFOLLOW those 5 links and NOFOLLOW everything else.

add_filter( 'custom_bbpress_nofollow', 'my_custom_bbpress_nofollow', 15, 2 );

function my_custom_bbpress_nofollow( $nofollow, $href ) {
$nofollow = '';
if ( strpos( $href, 'https://www.link1.com' )!== false || 
     strpos( $href, 'https://www.link2.com' ) !== false || 
     strpos( $href, 'https://www.link3.com' ) !== false ||
     strpos( $href, 'https://www.link4.com' ) === false
    ) {
   $nofollow = 'rel="nofollow" target="_blank"';
}

return $nofollow;
}
Liss
  • 15
  • 5