Skip to content

Instantly share code, notes, and snippets.

@foliovision
Created February 20, 2020 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foliovision/41e017261f5534418490d23e25091d7b to your computer and use it in GitHub Desktop.
Save foliovision/41e017261f5534418490d23e25091d7b to your computer and use it in GitHub Desktop.
Make wp_is_mobile() use WP Rocket mobile detection
<?php
/*
Core WordPress function wp_is_mobile() detects iPad as mobile, so we filter it and use WP Rocket mobile detection
*/
add_filter( 'wp_is_mobile', 'fv_bra_wp_is_mobile' );
function fv_bra_wp_is_mobile( $is_mobile ) {
if ( class_exists( 'Rocket_Mobile_Detect' ) ) {
$detect = new Rocket_Mobile_Detect();
// WP Rocket has a plugin which lets it change the detection to treat iPad as desktop, so we count on that possibility too
$rocket_cache_mobile_files_tablet = apply_filters( 'rocket_cache_mobile_files_tablet', 'desktop' );
if ( $detect->isMobile() && ! $detect->isTablet() && 'desktop' === $rocket_cache_mobile_files_tablet || ( $detect->isMobile() || $detect->isTablet() ) && 'mobile' === $rocket_cache_mobile_files_tablet ) {
$is_mobile = true;
} else {
$is_mobile = false;
}
}
return $is_mobile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment