17

I have developed responsive theme and I want to submit it the wordpress.org. Before submitting it I want to use wp_is_mobile() in my theme, but according to Function Reference/wp is mobile it is a bad idea because it say's this :

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.

So if I use it in my theme functions.php like this:

add_filter('body_class','mobile_theme_body_class');     
function mobile_theme_body_class( $classes ){

    if ( wp_is_mobile() ){
        $classes[] = 'mobile';
    }
    else{
        $classes[] = 'desktop';
    }
    return $classes;
}

Will my theme be rejected ?

Sub-question:

If I use my function like in the code above and use Caching Plugins like (WP Super Cache) is my function gonna be messed up (failed to execute or returning false positives) ?

Thank you for your time and answers...

  • 1
    They are saying, we have this function, but don't use it, because it isn't good - how utterly ironic. I don't know if your theme would rejected for using it, but if so, then it would increase the irony to a maximum. After all it is a WP function and not deprecated. – Nicolai May 20 '15 at 14:43
  • 3
    The real question is why you want to add the mobile/desktop class? – TheDeadMedic May 20 '15 at 14:44
  • 1
    What @TheDeadMedic says is true, you can work with with CSS media queries for example. – Nicolai May 20 '15 at 14:47
  • 1
    wp_is_mobile() is nothing more than a joke really and is really unreliable. It relies on user side which can be manipulated by the end user. You need to know that your functionality will fail in more than 10% of cases, so a large amount of mobile users will be dished desktop content whether or not they like it or you don't want it – Pieter Goosen May 20 '15 at 14:58
  • 4
    So it is there and is sucks just like query_posts(); – Dejo Dekic May 20 '15 at 15:06
31

In very layman term wp_is_mobile() is not for styling your theme.

How it works: It matches some of device's native name in User Agent String. So if someone manipulate the string and send false information you can not detect what device it is. And it does not return any device name it just return true if you are on not on desktop else false

How WordPress use it: WordPress do not use it for styling or adding CSS anywhere. WordPress use it to add or manipulate thing which should be only on mobile devices (regardless size and name.) e.g. to add touch scripts, add viewport, mobile class in admin header, adding mobile buttons.

Effect of caching: If you use caching plugin it is useless. As your code not executed every time so you get the same result every time.

WordPress mostly use it at back-end and almost every caching plugin excludes caching at back-end or say for logged-in user. So it works fine.

1

wp_is_mobile() is related to HTTP USER AGENT, it's not related to screen size

wp_is_mobile return true or false based on $_SERVER['HTTP_USER_AGENT']

if $_SERVER['HTTP_USER_AGENT'] contains Mobile( Android or BlackBerry or so on .. ),

then wp_is_mobile returns - true ( detected as a mobile device ), if not then it will return false

a simple use case - to make a link, try to navigate to Mobile App or Web page.

for more info check function source code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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