WordPress 3.4 is more “mobile-friendly” with the introduction of a new function: wp_is_mobile(). This function checks for a mobile user agent. Another way to say it is that the function checks to see if the browser you’re using to visite the site is on a mobile device. It looks for a blanket “Mobile” user agent, as well as specific ones: “Android”, “Silk”, “Kindle”, “BlackBerry”, and “Opera Mini”. This covers iPads, Android tablets, iPhones, Blackberries, Android phones, and more. The function only returns TRUE or FALSE. That’s it. You can use this like a conditional template tag to test for the presence of a mobile device.
How would you use this?
If you wanted to deliver (or not deliver) parts of your template conditionally, based on the presence of a mobile device, you could use wp_is_mobile() to make this happen. Note in the code below that you can test for the negative, as well. This could be useful when you need to specify content delivered for each condition.
Where would this go?
That depends on why you’re using it. Think of this as a conditional tag. Let’s say you have a site that uses Twenty Eleven as its theme. You want the search field in the header for non-mobile visitors, but for mobile visitors, you don’t want it up there. We’ll start in the header.php file in the twentyeleven directory (in wp-content/themes).
Look for this line of code: <?php get_search_form(); ?> (It should be around line 125.)
Edit that line to look like this:
Now the search form will only load in the header if the conditional is false. In other words, when the visitor to the site is on a non-mobile (desktop) browser.
You could use this to restructure your code in order to deliver more optimal content based on the browser. I did this recently for a client’s site. We wanted to move the main nav and change the behavior & appearance of the background based on what type of browser was being used:
Taking it further
On the above example, I showed a mobile phone view and a non-mobile view. What happens when you want to target tablets separate from phones? Mobble is a nice plugin that helps you do just that. Mobble gives you multiple conditionals. These are just four of them:
is_handheld(); // any handheld device (phone, tablet, Nintendo)
is_mobile(); // any type of mobile phone (iPhone, Android, Nokia etc)
is_tablet(); // any tablet device (currently iPad, Galaxy Tab)
is_ios(); // any Apple device (iPhone, iPad, iPod)
Now you can use Mobble’s is_mobile() conditional and target just phones, or is_tablet() to target just tablets, etc.