WordPress Detect Browser

Following function is used to detect user’s browser and can be used to apply specific style or scripts for different browsers. Function detects users browser and class to the wordpress body_class() function. Function needs to be added in functions.php

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;

And in your theme files you must have added body_class() to tag like

<body <?php body_class(); ?>>

Function will output like this if the browser is safari.

<body class="home blog logged-in safari">


WordPress Users Browser Detection.

Leave a Reply

Your email address will not be published. Required fields are marked *