How to detect mobile user in wordpress?

WordPress has a cool function to detect if user visiting your site from mobile or desktop.

wp_is_mobile

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

Do not think of this function as a way of detecting phones. Its purpose is not detecting screen width, but rather adjusting for the potentially limited resources of mobile devices. A mobile device may have less CPU power, memory and/or bandwidth available. This function will return true for a tablet, as it too is considered a mobile device. It is not a substitute for CSS media queries or styling per platform.

One way that this function could be used in a theme is to produce a very light version of the site that does not have the large payload of the desktop site. Note that both the desktop and the mobile versions of the page will still need to be responsive, as an older portrait phone will have a significantly different width than a modern iPad in landscape. wp_is_mobile() will be true for both. Similarly a desktop browser window may not be displayed at full width. Essentially this approach may double the amount of work you will need to put into the theme. Yet for a tightly optimized theme or a unique mobile experience, it may be essential. It also means that a proper theme may have at least three different responsive design specs: Desktop, Mobile and AMP.

Additionally, care must be taken when using this function in a public theme. If your theme works differently for mobile devices and desktop devices, any page caching solution used MUST keep separate mobile/non-mobile buckets. Many caching solutions do not do this or charge for this feature. Even the most detailed read me file may not be able to adequately explain these details.

How to use?

<?php
if ( wp_is_mobile() ) {
 /* Include/display resources targeted to phones/tablets here */
} else {
 /* Include/display resources targeted to laptops/desktops here */
}
?>
Is there any wordpress plugin for mobile detection?

yes, there is a mobile detect plugin available in wordpress plugin directory.

what is mobile detect plugin?

Conditional Display for Mobile is a mobile detect plugin that can be used to control what content is displayed depending on the visitor’s device or web browser. For example, you might want to display some content only on iPhone and iPad, or you want to hide the content on mobile and tablet devices. The plugin also supports add start time and end time to shedule the content so it will only be displayed in the specified date and time.

Features

  • Include or exclude devices
  • Detect iPhone, iPad, iPod, Android, Mobile, Windows, Linux and Mac
  • Include or exclude web browsers
  • Detect Mozilla Firefox, Google Chrome, Safari, Opera, Internet Explorer 6/7/8/9/10/11 and Microsoft Edge
  • Add start time and end time to schedule the content so it will only be displayed in the specified date and time
  • Support shortcode in the content
  • Use the conditional display shortcode in text widget
  • PHP functions to detect mobile and browser

If you need help with plugin or anything with wordpress, feel free to contact us.

Leave a Reply

Your email address will not be published.

*

code