WP user agent returns random variables

I am using a PHP function in order to return the user browser, using the WP global variables.

It used to work perfectly, but recently it started doing random things on Safari. I use chrome and everything is fine at home. My colleague uses Safari and depending on the test, he has different results.

On sidebar.php :

div class="sidebar s2 ?php echo 's2_'.detecter_user_agent(); ?"

In functions.php :

function detecter_user_agent() {
$cl = '';
global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_macIE, $is_winIE;

if($is_lynx) $cl = 'lynx';
elseif($is_gecko) $cl = 'gecko';
elseif($is_opera) $cl = 'opera';
elseif($is_NS4) $cl = 'ns4';
elseif($is_safari) $cl = 'safari';
elseif($is_chrome) $cl = 'chrome';
elseif($is_IE) $cl = 'ie';
elseif($is_macIE) $cl = 'macie';
elseif($is_winIE) $cl = 'winie';
else $cl = 'unknown';

return $cl;

}

This is the code to generate a CSS class for the right sidebar on https://blog.defi-ecologique.com

When my colleague tries it, it sometimes returns s2_chrome, s2_gecko or even s2_unknown.

I have no way of knowing how many of my readers have this issue.

Could you please help me make sure that the right global variable is returned at any time ?

Thank you !

Topic browser-compatibility globals Wordpress

Category Web


You can examine wp-includes/vars.php for specific logic which implements sniffing for these globals in WordPress.

In a nutshell the checks are very basic, especially considering user agent sniffing is inherently a mess.

If you need a more reliable implementation you would have to get one elsewhere (or code it yourself). That or use different technique entirely, such as detecting necessary context client–side with JavaScript.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.