Wednesday, June 15, 2011

Detect visitor browser within WordPress

How to detect the visitor browser within WordPress

To ensure maximum cross-browser compatibility on the wordpress blog theme, We have to able to detect the browser used by the blog visitor. WordPress can detect client browser. I describe the proces below.

Paste the code below in your functions.php file:


add_filter('body_class','browser_body_class');
function 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';

if($is_iphone) $classes[] = 'iphone';
return $classes;
}


Now saved the file and the function will automatically add a CSS class to the body tag, as shown in the exemple below:

body class="home blog logged-in safari"

So now with the help of the code, you just have to take your stylesheet, and add some browser-specific styles!

No comments:

Post a Comment

I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off