Excluding iPad from wp_is_mobile!

Mobile Detect Logo
Mobile Detect Logo by Dragoș Gavrilă!

wp_is_mobile function in WordPress considers iPad as mobile! This scenario creates issues on iPad, iPad Pro, or any relevant tablets. To be precise, iPads are shown a mobile version of a site rather than the desktop version! Searching the internet didn’t yield a clean way to overcome this situation. Heck, there are even plugins to detect mobile devices and to exclude iPads from being mobiles.

Most such plugins have one or the other disadvantages. One plugin comes with tons of shortcodes. Another plugin uses its own functions such as my_wp_is_mobile(). Here’s the actual code for wp_is_mobile in WordPress core

function wp_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
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
            $is_mobile = true;
    } else {
        $is_mobile = false;
    }
 
    /**
     * Filters whether the request should be treated as coming from a mobile device or not.
     *
     * @since 4.9.0
     *
     * @param bool $is_mobile Whether the request is from a mobile device or not.
     */    return apply_filters( 'wp_is_mobile', $is_mobile );
}

I wanted a solution that works along with wp_is_mobile without a hack! Fortunately, as with most other functions, wp_is_mobile uses apply_filters hook. So, it is easy to modify its behavior using add_filter!

In order to identify mobiles and tablets, there are a number of PHP libraries available. I found Mobile Detect to be more precise and actively maintained. It’s open source too! If you find this snippet useful, please consider making a donation directly to the author of Mobile Detect PHP Library.

Without any further ado, here’s the complete(!) code that can be used as a plugin for your site…

<?php

/**
 * Plugin Name: Mobile Detect
 * Version 1.0
 * Description: Fine-tune wp_is_mobile with mobile-detect PHP library.
 */
defined('WPINC') or die('No direct access allowed!');

if( ! class_exists( 'Mobile_Detect' ) )
    require_once( __DIR__ . '/Mobile-Detect-2.8.27/Mobile_Detect.php' );

$md = new Mobile_Detect;

if( $md->isMobile() && !$md->isTablet() )
    add_filter( 'wp_is_mobile', '__return_true' );
else
    add_filter( 'wp_is_mobile', '__return_false' );

Please make sure to download the Mobile Detect PHP library and keep it in a sub-directory. As of this writing, the latest version of MobileDetect PHP library is 2.8.27. So, I’ve used its version number as part of the directory name! YMMV!

MobileDetect library comes with plenty of helper functions (apart from the basic detection methods… isMobile and isTablet). There is isiOS, isAndroid, is(‘Chrome’). You may even set User-Agents, if your plugin uses WordPress HTTP API!

Activate the plugin and now, wp_is_mobile should work as expected for iPads!

This plugin is now accepted into the official WordPress plugins repo. Check it out at TinyWP Mobile Detect Plugin.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *