X

How To Check If the Site Visitor Is Using a Mobile Device

Snippets by IsItWP

Are you looking for a way to check if the site visitor is using a mobile device? This snippet checks to see if the user is visiting using a mobile device and returns the result as true or false.

Instructions:

  1. Add this code to your theme’s functions.php file or in a site-specific plugin.
  2. // Add to functions.php
    // version proof, checks if the visitor is from a mobile device
    function muneeb_wp_is_mobile() {
    
        if ( function_exists( 'wp_is_mobile' ) )
            return wp_is_mobile();
    
        //code from wp_is_mobile function, wp_is_mobile() is located in wp-includes/vars.php version 3.4
        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;
    
    }
    
  3. Then, use this code in your theme template to check if a visitor is using a mobile device.
  4. // Use anywhere
    if ( muneeb_wp_is_mobile() ){
     //do mobile stuff here
    }
    

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 9 Best SEO Tools to Grow Your Website Traffic, FAST!

[code]// Add to functions.php // version proof, checks if the visitor is from a mobile device function muneeb_wp_is_mobile() { if ( function_exists( 'wp_is_mobile' ) ) return wp_is_mobile(); //code from wp_is_mobile function, wp_is_mobile() is located in wp-includes/vars.php version 3.4 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; } // Use anywhere if ( muneeb_wp_is_mobile() ){ //do mobile stuff here }[/code]

Comments   Leave a Reply

  1. Great job, thanks

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!