How to Detect Visitor Browser Type in WordPress With PHP Browser Detection Plugin

There are so many methods to serve specific data to certain web browsers. For example, we can use conditional comments for like below:

 

[code type=”html”]

anything

[/code]

We can also use JavaScript browser detect function. See here.

Modernizr is also here for us for patching the stuff that are not supported to certain browsers. Oh I forgot to mention, CSS Media Query, very useful for mobile browsers.

Note: This tutorial is just for WordPress. Note for custom PHP.

Above mentioned methods are client side methods. Sometimes, these are not the ideal solution. As this is a WordPress tutorial for detecting browser with server-side language “PHP“. You can download the plugin here.

This plugin is very handy and easy to use. Just you’ll have to install the plugin and tweek your template file.

In your template file, if you just needed all the information in a variable or array, just do like below:

//Get all information in array
$browserInfo = php_browser_info();
//Get browser version number
$browserInfo = get_browser_version();
//Get browser name
$browserInfo = get_browser_name();

You can also use template tags in your theme. See below:

For mobile

if(is_mobile()) { }

if(is_ipod()) { }

if(is_ipad()) { }

if(is_iphone()) { }

For browsers

Here $version is optional variable. You can leave it empty.

if(is_ie($version)) { /* anything */ };

if(is_opera($version)) { /* anything */ };

if(is_chrome($version)) { /* anything */ };

if(is_firefox($version)) { /* anything */ };

if(is_safari($version)) { /* anything */ };

Conditions for greater or less than a specific browser version

if(is_safari() && get_browser_version() >= 4) { /* anything */ };

if(is_ie() && get_browser_version() <= 10) { /* anything */ };

if(is_firefox() && get_browser_version() <= 19) { /* anything */ };

Tip: For custom PHP projects, you can use PHP get_browser() function.

Duan Lingxin

Duan Lingxin

Content crafter and chief editor at Scratching Info. Also regular contributor on other major online tech platforms. Security Specialist by day and a writer by night, he does his best to instill his knowledge about tech while delivering inspiring and life changing resources through his writing,

More Posts - Website

Leave a Comment