17

I'm going to test the user agent to load a mobile template instead of the desktop theme.

There are many mobile detection scripts out there.

  • Does wp_is_mobile() function work well?
  • What is your experience in comparison with other classes like e.g. mobiledetect.net?

4 Answers 4

16

Yes it works well. It's a very simple function but never found a mobile device not recognized by it. It recognize the 90%+ of mobile devices. Main difference from mobiledetect.net is that doesn't differe from phone and tablets.

See the code

1
  • But as hint - 90% is not all, and 90% is only today. We get very fast new device and this function is often not helpful. You exclude devices, users from the solution.
    – bueltge
    Commented Nov 18, 2015 at 11:05
11

Yes, the wp_is_mobile() works well, but you can run into problems when using aggressive caching systems, such as fastcgi, proxy cache, etc. that skip the php execution.

If that is the case, I would recomend some client side (javascript) detection method that is loaded for both mobile and desktop browsers.

1
  • That's a good point. If you use full page caching in auto (per request) mode, the cache will contain mobile OR desktop version, depending on what device the first request was made from. All further requests will be served with this version, no matter what device type it is. If you use a pre-caching solution (which I always do), you'll get desktop versions generated and mobile users will always get them. Commented May 8, 2015 at 14:53
8

It is a shitty idea to use that function. Device identification should always be done at the client side, and user agent is probably the worst way to identify whether a device supports a feature X or have a form factor Y.

You start using it when the site is young and then your site grows and you decide to cache your HTML and boom all your device detection code stops working..... Better not to walk down that ally in the first place.

1
  • I upvoted your answer because consideration about cache are very important in my opinion too. Commented Mar 29, 2018 at 8:11
1

Your question specifically asks if wp_is_mobile() is effective; not how device detection should be evaluated. The function just works. So Yes, it is effective. As an example when it may be preferred to client-side detection, even Mozilla thinks that's a bad idea.

It's worth re-iterating: it's very rarely a good idea to use user agent sniffing. You can almost always find a better, more broadly compatible way to solve your problem! mozilla

Is a mobile dectection plugin any better? There may be good reason not to choose mobile detection via a plugin over wp_is_mobile(). First, Wordpress is already doing a lot of the tedius tasks, so just hand this over to it as well!

Prior to loading a DataTable I am using mobile-detect.js as part of my requirement to detect and style when the device is mobile:

// if phone or tablet then set responsive mode
var md = new MobileDetect(window.navigator.userAgent);
if (md.phone() || md.tablet()) {
    scrollObj = false;
    $('#sometable').removeAttr('width').addClass('dt-responsive');
    responsiveObj = {
        details: {
    type: "inline",
    display: $.fn.dataTable.Responsive.display.childRowImmediate
        }
    };
}

This works fine, but it comes with a small cost to require a plugin for a one time use per session function call. Additionally, it's creator warns about using his plugin.

"you should not use this library in your HTML page and it’s less reliable when used server-side"

So, instead of loading a one-time-use plugin, a more effective way to handle device detection without sniffing is to use wp_is_mobile().

// This is in a JavaScript block in a PHP page
isMobile = Boolean(<?php echo wp_is_mobile() ?>);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.