WordPress global variables to determine the user's browser and determine the IE browser version of the method

In the process of WordPress theme development, we need to write some CSS for some old browsers (Internet Explorer) according to the different browsers, in order to solve the compatibility problem, WordPress provides us with global variables for determining the browsers used by site visitors, these variables return a boolean value.

All variables provided by WordPress to determine the user's browser

$is_iphone (boolean) iPhone Safari
$is_chrome (boolean) Google Chrome
$is_safari (boolean) Safari browser
$is_NS4 (boolean) Netscape Browser
$is_opera (boolean) Opera Browser
$is_macIE (boolean) Internet Explorer for the Mac platform
$is_winIE (boolean) Internet Explorer for Windows platforms
$is_gecko (boolean) FireFox Browser
$is_lynx (boolean) Plain text browser for Linux systems
$is_IE (boolean) Internet Explorer

Why are there no domestic browsers such as 360 and Sogou? Because these browsers use the so-called IE, Google Chrome dual kernel, depending on the mode of use will be judged as Google or IE browser.

How to determine the version of IE browser?

The above code can determine which browser the user is using? But it can't determine the version of the browser the user is using, thanks to the convenient update mechanism, other browsers will generally be updated to the latest version, and the latest version of the nearest versions of the rendering performance is similar to the latest version of the browser, IE has been the odd one out, the existing IE browser from IE6 to IE11, each version of the performance is very different, so it is very necessary to determine the version of the IE browser. So it is necessary to determine the version of IE browser. WordPress does not provide a function to determine the version of IE browser, so we have to do it ourselves.

$browser = $_SERVER['HTTP_USER_AGENT'];
$browser = substr( "$browser", 25, 8);

if ($browser == "MSIE 7.0" ) {
    $classes[] = 'ie7';
    $classes[] = 'ie';
} elseif ($browser == "MSIE 6.0" ) {
    $classes[] = 'ie6';
    $classes[] = 'ie';
} elseif ($browser == "MSIE 8.0" ) {
    $classes[] = 'ie8';
    $classes[] = 'ie';
} elseif ($browser == "MSIE 9.0" ) {
    $classes[] = 'ie9';
    $classes[] = 'ie';
} else {
    $classes[] = 'ie'; } else { $classes[] = 'ie'; }
}  

We can put the browser version of the judgment into the page body tags, and then for a buggy browser specifically write CSS, and when the browser completely disappeared from the Internet, directly for the browser to write styles can be deleted. The advantage of doing this is that it reduces the CSS Hack and makes maintenance much easier.

Related Posts

Leave a Reply

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