Created
February 20, 2020 14:07
-
-
Save foliovision/41e017261f5534418490d23e25091d7b to your computer and use it in GitHub Desktop.
Make wp_is_mobile() use WP Rocket mobile detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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