Post Pic

How to detect the visitor browser within WordPress

To ensure maximum cross-browser compatibility on your blog theme, you have to be able to detect the browser used by the visitor. Sure, you can use conditionnal comments, but WordPress itself can detect client browser. Let’s see how to do.

Simply paste the code below in your functions.php file:

<?php
add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
	global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

	if($is_lynx) $classes[] = 'lynx';
	elseif($is_gecko) $classes[] = 'gecko';
	elseif($is_opera) $classes[] = 'opera';
	elseif($is_NS4) $classes[] = 'ns4';
	elseif($is_safari) $classes[] = 'safari';
	elseif($is_chrome) $classes[] = 'chrome';
	elseif($is_IE) $classes[] = 'ie';
	else $classes[] = 'unknown';

	if($is_iphone) $classes[] = 'iphone';
	return $classes;
}
?>

Once you saved the file, the function will automatically add a CSS class to the body tag, as shown in the exemple below:

<body class="home blog logged-in safari">

That's all. You just have to take your stylesheet, and add some browser-specific styles!

Credits goes to Nathan Rice for this awesome trick!

Related Posts

Related Posts

No related posts.

40 Responses

Jun 15 2009 10:06

This is a nice solution – very simple to extend. Any notes on the payload and performance against existing methods?

Jun 15 2009 10:54

Thanks for this recipe. :)

But, seeing that I always format my IDs and classes with a dash in between words i.e. #my-id or .my-class, how do I get this cope snippet to print out the class with dashes in between the words like:

or to even change the class name to something to my own liking?

Jun 15 2009 10:56

/sigh

WordPress wouldn’t print the code I tried to insert in the last coment. I’ll try again:

Thanks for this recipe. :)

But, seeing that I always format my IDs and classes with a dash in between words i.e. #my-id or .my-class, how do I get this cope snippet to print out the class with dashes in between the words like:

home-blog-logged-in-safari

or to even change the class name to something to my own liking?

Jun 15 2009 11:44

this is something great. Thanks

Jun 15 2009 15:18

Nice little hack. Very useful considering the increasing use of mobile browsers

Jun 15 2009 15:38

Nice trick!

BTW, is there any similar way to easily detect the visitor language?

Jun 15 2009 18:24

Besides this, WordPress 2.8 now includes it’s own body class. Just use the following function:

<?php body_class(); ?>

Jun 15 2009 20:47

Great,
I will try this on my wordpress website.

Jun 16 2009 03:36

@TheFrosty That’s what this is. It’s just using the actual filter instead of the template tag.

Jun 16 2009 18:32

That’s a nice tip but why would people want to track browsers? Maybe I am missing something.

Jun 17 2009 11:43

Nice trick. Like the idea.

Jun 17 2009 16:03

The Php code that you mentioned above is really helpful. I have implemented it and the results are very good. Thanks for such a nice post..

Jun 18 2009 18:59

Maybe I’m missing it but where are you pulling the browser info from?

Jun 22 2009 17:46

Of course, this is only reliable if you can trust the viewer to not spoof the user-agent string.

Jun 25 2009 17:59

can you show me an example of how you would use it in your .css file? i’m not sure how to do that.

Jun 25 2009 19:53

@shane: you’ll do:
body.safari{
/* this will only be interpreted by Safri*/
}

Jul 06 2009 18:12

Hmm. Is it true? :-)

Jul 09 2009 17:30

I greatly appreciate this code. Very useful indeed!
Thank you

Jul 20 2009 04:46

I’ve seen so many people code portions of their code towards specific browsers, I think it’s a great idea to do it, this code will help aton to achieve this on my own theme =D

Till then,

Jean

Aug 31 2009 02:11

Now all Wordpress needs to include is version detection and I can stop including my custom function in every theme i create :)

Sep 14 2009 17:50

Hurrah, great for giving IE6 users an upgrade your browser message ;)

Dec 11 2009 18:50

This is good and i learned a method to achieve my goal but this is not 100% usable(as for as i concern). mainly, i use browser detection class to detect different version of IE. If your website looks good in Firefox, 80% chance are your website will be look same in other browser except different version of IE. so as a programmer i need to get browser name as well as version number so i can code accordingly. Is Wordpress has some global variable for browser version?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required