Skip to content

Instantly share code, notes, and snippets.

@ingozoell
Created January 21, 2014 13:41
Show Gist options
  • Save ingozoell/8540255 to your computer and use it in GitHub Desktop.
Save ingozoell/8540255 to your computer and use it in GitHub Desktop.
Add Browser-Detection to Wordpress body_class
// Add Browser-Detection
function mv_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';
if(preg_match('/MSIE ([0-9]+)([a-zA-Z0-9.]+)/', $_SERVER['HTTP_USER_AGENT'], $browser_version))
$classes[] = 'ie'.$browser_version[1];
} else $classes[] = 'unknown';
if($is_iphone) $classes[] = 'iphone';
if ( stristr( $_SERVER['HTTP_USER_AGENT'],"mac") ) {
$classes[] = 'osx';
} elseif ( stristr( $_SERVER['HTTP_USER_AGENT'],"linux") ) {
$classes[] = 'linux';
} elseif ( stristr( $_SERVER['HTTP_USER_AGENT'],"windows") ) {
$classes[] = 'windows';
}
return $classes;
}
add_filter('body_class','mv_browser_body_class');
@james2doyle
Copy link

james2doyle commented Jun 15, 2017

Here is a version in much less lines using the native WordPress globals https://gist.github.com/james2doyle/979b09fc7c676baf3283bdb113b3db6d

It doesn't add the device though, just the browser name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment