Is wp_is_mobile() effective?

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?

Related posts

4 comments

  1. 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

  2. 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.

  3. 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.

  4. 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() ?>);
    

Comments are closed.