3

Let me know whether wp_is_mobile function will work with all the mobile and tablet devices ? Is it enough to check mobile device ?

I found that this method in wp-includes/vars.php

    function wp_is_mobile() {
    static $is_mobile;

    if ( isset($is_mobile) )
        return $is_mobile;

    if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
        $is_mobile = false;
    } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false ) {
            $is_mobile = true;
    } else {
        $is_mobile = false;
    }

    return $is_mobile;
}

If i need to develop any mobile site, whether i can use this method ?

Edit

Why wp uses server side script to detect mobile device ?

1 Answer 1

5

No, it will fail. There are literally thousands of mobile clients, and not all send a usable User-Agent header. There are better ways to adjust the site to small screens (which is not necessary the same as mobile!).

Use client side detection and offer a link to switch the view; save that option in a cookie.

I think the common misconception is that mobile == small screen. The Nexus 10 has more pixels than my big desktop screen, while my old laptop running a desktop Ubuntu shows just a 1024x768 pixels. Surface RT comes with 1366x768 pixels, a mouse (trackpad), a keyboard and a touch interface. And some UAs are just not detectable.

Which one will get the layout “optimized for mobile”?

3
  • 1
    Then why wp uses server side script to detect mobile device ?
    – Miqdad Ali
    Nov 21, 2012 at 3:34
  • No idea. Nobody said the core code is perfect. :)
    – fuxia
    Nov 21, 2012 at 3:36
  • 1
    Looks like the core uses wp_is_mobile to adjust body classes in the admin area and also determine if a given browser can upload thing. shell$ ack wp_is_mobile will give you some insight -- not that any of those things are really a good reason to use it, but perhaps it was the best solution at the time. Nov 21, 2012 at 4:31

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.