WordPress Browser Body Classes Function


Browser sniffing can get you out of trouble in one or other occasion and thats makes it worth the attention. This is a little function I borrowed from Nathan Rice that does the job pretty well. I added some code from the Krusty classes plugin to have IE versions as classes for more specific targeting. Hope you find it useful, enjoy!

/*
|--------------------------------------------------------------------------
| BROWSER CLASSES
|--------------------------------------------------------------------------
*/
if ( ! function_exists( 'mv_browser_body_class' ) )
{
	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';
		return $classes;
	}
	add_filter( 'body_class','mv_browser_body_class' );
}

Reactions (2)

  1. Great snippet. By rights, this should find its way into the WordPress core.