Archived topic
No background video on Mobile
1 reply · Started by Brenden on May 13, 2022
I'm using this https://docs.generatepress.com/article/page-hero-background-video/, but I want to not load the video on mobile. I turned the html off on mobile, but according to Google PageSpeed the video is still loading. What is the best way to not load the video on mobile?
Hi Brenden,
You can give wp_is_mobile() a try, this is a WP function, for more info:
https://developer.wordpress.org/reference/functions/wp_is_mobile/
I would suggest duplicate the current page hero, so there will be 2 elements, 1 with video for desktop and 1 without video for mobile.
Then you can try this php snippet, replace 100 with the desktop element ID, replace 200with the mobile element ID.
add_filter( 'generate_element_display', function( $display, $element_id ) {
if ( (100 === $element_id && wp_is_mobile()) || (200 === $element_id && !wp_is_mobile()) ) {
$display = false;
}
return $display;
}, 10, 2 );