Skip to content

Instantly share code, notes, and snippets.

@fencermonir
Forked from james2doyle/browser-body-class.php
Created August 28, 2018 07:56
Show Gist options
  • Save fencermonir/7844ab60312d60f55fdf44c43839c2bd to your computer and use it in GitHub Desktop.
Save fencermonir/7844ab60312d60f55fdf44c43839c2bd 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.
<?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