Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mobile detection for NGINX Synthesis accounts
<?php
/**
* Basic mobile detection for Synthesis accounts with NGINX caching.
*
* @since 1.0.0
* @return boolean
*/
function ja_is_mobile() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
return false;
$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
$mobile = false;
$mobile_devices = array( 'android', 'iphone', 'ipod', 'winphone8' );
$tablet_devices = array( 'tablet', 'tablet-android' );
// Check for mobile device user agent
foreach ( $mobile_devices as $device ) {
if ( false !== strrpos( $agent , $device ) ) {
$mobile = true;
}
}
// Check for tablet user agent part, which will override the first check
foreach ( $tablet_devices as $device ) {
if ( false !== strrpos( $agent , $device ) ) {
$mobile = false;
}
}
return $mobile;
}
/**
* Load mobile changes and tweaks if needed.
*/
if ( ja_is_mobile() ) {
require_once( CHILD_DIR . '/inc/mobile.php' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment