Site logo

Archived topic

Hide sidebar with menu, if no password is given ...

5 replies · Started by Mads on April 22, 2021

Viewing posts 1–6 of 6

Hi,

I am working on a new site.
It will have some public accessable pages, setup as 'full width'.
And also some pages, all with password-protection (basic WP/same password), setup as 'page with sidebar', where the sidebar shows a menu of all protected pages.

My challenge is that even the page it self is protected, the sidebars shows the menu...
Of course without password you can not access the pages. But the menu is shown to everyone...
I dont want to show a menu ogf hidden pages, it makes no sense....

So I found a solution where I can hide sidebar for users without passwords :-)
I needed to locate the template file with the get_sidebar(); to paste this code:

// If right sidebar, hide it.
if ( ! post_password_required()) {
get_sidebar();
}

It was not easy to locate the template file, and maybe my problem is that I found the wrongone (even it wotks half)...
At last I found that ind templatefolder/inc/structure/sidebars.php I can paste the code, and get it to work...
I had to remove this code to get it to work:

// If right sidebar, show it.
if ( in_array( $layout, $rs ) ) {
get_sidebar();
}

This way I succeded to hide sidebar on pages that are protected! :-)

But strangely, now my 'full-width'-pages are shown WITH a sidebar, AND showing the menu for everyone....

You can see it here on my developing-site: http://flinkelink.dk/amar/

As public visitor you are shown menu to protected pages....
All items in the TOP-menu is public (supposed to be...) full-width-pages.
But they are shown with sidebar and my "second menu for protected pages"...

The 'Login'-page ask for a code, and show up as expected without the sidebar (even in backend, it is set as 'page with sidebar', it is correctlyly hidden before passcode). After correct code the 'Login'-page shows the protected content AND the sidebar with the menu. All as expected.

So I solved to hide the sidebar om protected pages for public users :-)
But public fullwidth-pages are shown with sidebar? :-(

I will appreciate very much any ideas to solve this challenge.
Either by correcting my way/code, can you 'combine' the two codes?
Or by by having a different 'take' on how to solve.

:-)

Hi there,

Editing core files like this is never recommended.

Instead, you should do something like this:

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

    return $layout;
} );

That should have the same effect without any negative sides and doesn't involve editing core files.

Hope it helps!

Hi Tom,
Thanks for getting back to me!
And yes, I know that this is not 'best practice'...
So thanks for giving me another approach :-)

I assume the code you provide is CSS. Right?
So I added it through costumizer 'additional css'.
But it does not have the wanted effect. :-(

I am back to showing all public pages (full-width) correct again :-)

But code-protected pages (page w. sidebar containg sec.menu) is still showing the "secret sidebar & menu"
You can see it here: http://flinkelink.dk/amar/medlemssiderne/

Am I wrong assuming that the provided code is CSS, and intended to be used in costumizer->additional css?
I am not strong in code and programming, but expert in copy/paste :-)

:-) Mads

Hi David,
Thanks for clarifying!
I have used the 'code snippet' plugin, and it works perfect now!

Thanks for the support!

:-) Mads

Glad we could be of help

This archived topic is closed to new replies.