Dynamic Web Lab
Back to insights

Add visitor browser name in the body class function

September 14, 20121 min read

You can easily detect browser using this function and add visitor browser name in the body class function .

Put this on your theme functions.php

[php]
add_filter(‘body_class’,’myfunction_browser_body_class’);
function myfunction_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’;
else $classes[] = ‘unknown_browser’;

if($is_iphone) $classes[] = ‘iphone’;
return $classes;
}
[/php]

The sample output may like this

[php]<body class="home logged-in ie">[/php]

Source: Snipplr.com

Writer

Dynamic Web Lab Editorial

We share how we design, engineer, and scale digital products across the GCC, Europe, and the US.

Share this article

Tags

wordpress

Need help implementing this?

We turn these playbooks into shipped features. Let us scope your roadmap and support your team.

Start a project conversation