Site logo

Archived topic

Hide Right-Sidebar Mobile View with PHP function

3 replies · Started by Alejandro on April 8, 2021

Viewing posts 1–4 of 4

Hi,
I want to hide the right sidebar in mobile view through php.
I've tried this function to work with no results:

add_filter( 'generate_sidebar_layout', function( $layout ) {
    if ( is_single() && wp_is_mobile() ) {
        return 'no-sidebar';
    }
    return $layout;
 } );

Checked it on an Iphone 6 and One Plus 6T already.

Could you give me a hand?

Thank you

Hi there,

the problem with the wp_is_mobile function is that it won't surpass Page caches. If the server ( or browser or router or ISP ) has cached the Desktop page then it will serve that file and the function doesn't do anything.

First place to check is whether your server has page caching enabled and whether it provides a separate page cache for mobile devices.

Awesome David.
I am using Varnish, so I split both caches (desktop & mobile) and the snippet works flawlessly.

Bravo!

Glad to hear that!

This archived topic is closed to new replies.