Detect Browser

If you are using different stylesheets for different browsers theres a different way of detecting the users browsers than the HTML conditional-tags.

Now a class with the name of the browser will be added to your body-tag. Like this:

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

How to use it?

body.ie #mydiv {<br />
  /* Here goes IE fix */<br />
}<br />

Code Snippet:

<?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&#91;&#93; = 'lynx';
	elseif($is_gecko) $classes&#91;&#93; = 'gecko';
	elseif($is_opera) $classes&#91;&#93; = 'opera';
	elseif($is_NS4) $classes&#91;&#93; = 'ns4';
	elseif($is_safari) $classes&#91;&#93; = 'safari';
	elseif($is_chrome) $classes&#91;&#93; = 'chrome';
	elseif($is_IE) $classes&#91;&#93; = 'ie';
	else $classes&#91;&#93; = 'unknown';
	if($is_iphone) $classes&#91;&#93; = 'iphone';
	return $classes;
}
?>

Posted in Conditional