Detect Mobile devices With `wp_is_mobile` Function

Posted on by

There is very simple and easy to use WordPress function available,which lets you detect mobile devices. So if you want to Detect Mobile devices WordPress has a simple and easy to use function. With this function you can easily show or hide content on mobile devices.

For example you can hide large feature images for mobile devices and only people visiting your WordPress website from desktops will see large feature images.

What is WP is Mobile (wp_is_mobile) ?

I have been using this WordPress conditional tag for a while, called wp is mobile wp_is_mobile to hide some content on small screen devices. You can easily hide extra widgets, large images or extra menus for mobile devices.

This Conditional Tag (wp_is_mobile) checks if the user is visiting your WordPress website using a mobile device. This is a boolean function, meaning it returns either TRUE or FALSE. Works through the detection of the browser user agent string ($_SERVER[‘HTTP_USER_AGENT’]). source

How to hide content on mobile devices in WordPress

With wp_is_mobile conditional function you can easily display a special menu or message for mobile visitors. You can create separate widgets areas for mobile an desktop users as well. You can hide extra widget areas for mobile visitors.

How to use WP is Mobile (wp_is_mobile) function?

You can use following function to display content for mobile devices.

<?php
if ( wp_is_mobile() ) {
/* Display and echo mobile specific stuff here */
}
?>

Display special message for mobile visitors

Simply add this function in your theme file. You can put this code on header.php, sidebar.php, single.php or any other theme file.

<?php
if ( wp_is_mobile() ) {
echo "Message for mobile visitors";
}
?>

Warning

According to WordPress codex page, You should realize that this does not detect a mobile phone specifically, as a tablet is considered a mobile device. Check the Plugins area for several helpful alternatives. It also should not be used for themes.

There are many WordPress mobile detection plugins available. You can use those plugins. To hide some text or for other simple changes you can use CSS.

Function Reference

You can also visit WordPress Codex page :Function Reference/wp is mobile

One response on “Detect Mobile devices With `wp_is_mobile` Function

Leave a Reply

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