Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active January 30, 2020 22:01
Show Gist options
  • Save james2doyle/979b09fc7c676baf3283bdb113b3db6d to your computer and use it in GitHub Desktop.
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.
<?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