WordPress: How to disable sidebar on mobile devices using PHP

Saul Goodman September 15, 2019 Loading...

I think using CSS @media query to hide the sidebar is not a good idea because the sidebar content will still be loaded even it is not used. So, I will disable the sidebar by using a WordPress built-in function, wp_is_mobile(). Note that this method works well with WP Super Cache plugin.

<?php
//Only call the sidebar on desktop
if(!wp_is_mobile()) :
    get_sidebar();
endif;
?>

In order to make this work with WP Super Cache Plugin, go to Installed Plugins > WP Super Cache > Settings > Advanced and check the box next to “Mobile Devices Support “.

avatar
Welcome to KINDACODE. Have a nice day and code well!