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 !

  • 2
    it is a bad idea to do browser detection on server side. insert your classes with JS – Mark Kaplun Sep 30 '16 at 15:16
  • 1
    +1 @MarkKaplun, I think this question just highlights how unreliable server side browser sniffing can be. – Andy Macaulay-Brook Sep 30 '16 at 17:11
  • Thank you, I will try. would you have a good tutorial to suggest ? – Grégoire Llorca Oct 3 '16 at 9:22

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.

| improve this answer | |
  • Thank you for your help. Unfortunately, every time I tried with JS I have been failing miserably. How would you proceed ? Would you have a tutorial to suggest ? – Grégoire Llorca Oct 3 '16 at 9:22
  • Not my area of expertise, sorry. – Rarst Oct 3 '16 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.