Last active
January 30, 2020 22:01
-
-
Save james2doyle/979b09fc7c676baf3283bdb113b3db6d to your computer and use it in GitHub Desktop.
Add a custom body class to WordPress for the current browser being used. No external frameworks or plugins.
This file contains 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
<?php | |
function custom_body_classes($classes) | |
{ | |
// the list of WordPress global browser checks | |
// https://codex.wordpress.org/Global_Variables#Browser_Detection_Booleans | |
$browsers = ['is_iphone', 'is_chrome', 'is_safari', 'is_NS4', 'is_opera', 'is_macIE', 'is_winIE', 'is_gecko', 'is_lynx', 'is_IE', 'is_edge']; | |
// check the globals to see if the browser is in there and return a string with the match | |
$classes[] = join(' ', array_filter($browsers, function ($browser) { | |
return $GLOBALS[$browser]; | |
})); | |
return $classes; | |
} | |
// call the filter for the body class | |
add_filter('body_class', 'custom_body_classes'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment