Pages Menu
TwitterRssFacebook
Categories Menu

Posted by in Wordpress Tutorials | 0 Comments

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:

 

You might find these useful

Select Code
1
2
3
<!-- [if IE 7]-->
anything
<!--[endif]-->

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() &amp;&amp; get_browser_version() &gt;= 4) { /* anything */ };

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

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

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

Post a Reply

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



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>