admin管理员组

文章数量:1289875

I have developed responsive theme and I want to submit it the wordpress. 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...

I have developed responsive theme and I want to submit it the wordpress. 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...

Share Improve this question asked May 20, 2015 at 14:27 Dejo DekicDejo Dekic 9091 gold badge11 silver badges28 bronze badges 7
  • 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 Grossherr Commented May 20, 2015 at 14:43
  • 3 The real question is why you want to add the mobile/desktop class? – TheDeadMedic Commented May 20, 2015 at 14:44
  • 1 What @TheDeadMedic says is true, you can work with with CSS media queries for example. – Nicolai Grossherr Commented May 20, 2015 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 Commented May 20, 2015 at 14:58
  • 4 So it is there and is sucks just like query_posts(); – Dejo Dekic Commented May 20, 2015 at 15:06
 |  Show 2 more comments

2 Answers 2

Reset to default 34

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.

wp_is_mobile() detects device based on 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, IOS ... ) or Tablet its returns true else return false
if $_SERVER['HTTP_USER_AGENT'] is null return false

for more info check function source code

本文标签: mobileProper usage of wpismobile()