简体   繁体   中英

How to mitigate server-dependent behavior for REQUEST_URI to use it to suppress a specific shortcode

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)?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM