Created
January 2, 2017 09:48
-
-
Save tankbar/3953796cd56c05771ff6cbc7e5ab5f01 to your computer and use it in GitHub Desktop.
WordPress body class browser and OS detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Browser and OS detection | |
* @link http://www.wpbeginner.com/wp-themes/how-to-add-user-browser-and-os-classes-in-wordpress-body-class/ | |
*/ | |
function dxms_browser_os_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', 'dxms_browser_os_body_class' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment