Skip to content

Instantly share code, notes, and snippets.

@almazka987
Created July 23, 2019 16:05
Show Gist options
  • Save almazka987/7e44552a3e86416cdbe5f1573f827cb2 to your computer and use it in GitHub Desktop.
Save almazka987/7e44552a3e86416cdbe5f1573f827cb2 to your computer and use it in GitHub Desktop.
Mobile detect php get mobile and tablet classes for <body> including iPad detection

Use mobile detect php library in my WordPress projects

In functions.php:

require_once( get_stylesheet_directory() . '/lib/Mobile_Detect.php' );

In header.php:

<?php
	$detect = new Mobile_Detect;
	$mobile = $detect->isMobile() && ! $detect->isTablet();
	$tablet = ( $detect->isTablet() ) || (! $mobile && $detect->version( "iPad" ) );
	$mob_class = ( $mobile ) ? ' mobile' : '';
    $tab_class = ( $tablet ) ? ' tablet' : '';
?>
<!DOCTYPE //....

<body <?php body_class( 'alio' . $mob_class . $tab_class ); ?>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment