Quantcast
Channel: MUNEEB IT CONSULTANCY
Viewing all articles
Browse latest Browse all 6

PHP and WordPress Mobile Detector wp_is_mobile

$
0
0

For my WordPress Simple Popup Plugin, I was in search of PHP mobile detector which can check if the visitor is from a mobile device. I was lucky recently a function wp_is_mobile is added to the WordPress core in version 3.4 the problem is not all WordPress sites have been upgraded. Only 21% of total users as of writing have upgraded to the version 3.4 http://wordpress.org/about/stats/

The code snippet below can be used with all the WP versions to detect mobile devices. If the visitor is from mobile device it’ll return true if not then false. You can also use the function outside of WordPress environment.

Version proof wp_is_mobile

<?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 WP 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;

}

if ( muneeb_wp_is_mobile() ){
 //do mobile stuff here
}

?>

The post PHP and WordPress Mobile Detector wp_is_mobile appeared first on Muneeb.


Viewing all articles
Browse latest Browse all 6

Trending Articles