-1

I noticed something quirky regarding when I put the following line in my PHP code:

echo strpos($_SERVER['REQUEST_URI'], "/?s=");

If the following URL is entered: https://s1.temporary-access.com/~allacros/devel/?s=tahiti the echo statement returns '16'. That's expected behavior.

However, if the following URL is entered: http://stage.world-of-waterfalls.com/?s=tahiti the echo statement returns '0'.

That's unexpected behavior, and it has consequences in that I cannot seem to use the following code to suppress the shortcode that causes an erroneous button artifact caused by that shortcode for search results pages (the only case I've seen where I'm trying to rely on this)...

if( !( empty( strpos($_SERVER['REQUEST_URI'], "/?s=") ) ) )
    remove_shortcode('uwr-button');

Anyone know why this is happening? And how to fix the code so it's agnostic to the server it's on (assuming that's the issue)?

  • 1
    Why not use the `strpos` function correctly? `0` is a valid result for a needle at the start of your haystack. Compare to a boolean `false` to see if it is not there. http://php.net/manual/en/function.strpos.php – ivanivan Dec 30 '18 at 21:34

1 Answers1

0

According to the documentation, strpos returns false if the needle was not found. So you can rewrite your condition like this:

if (strpos($_SERVER['REQUEST_URI'], "/?s=") !== false) {
    remove_shortcode('uwr-button');
}

Also, you can check s key existence in $_GET array:

if (array_key_exists('s', $_GET)) {
    remove_shortcode('uwr-button');
}

And a little advice: try to understand the difference between $_SERVER['REQUEST_URI'] and $_SERVER['QUERY_STRING'], because it seems like you need the second in your statements

Jekys
  • 313
  • 1
  • 8