Proper usage of wp_is_mobile()?

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…

2

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.

Leave a Comment