Black Friday Sale! Get up to $40 off GP Premium!Learn more

[Resolved] hide page sidebar until password entered

Home Forums Support hide page sidebar until password entered

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #306320
    Peter

    Hello Tom –

    I have GP running a public website with two pages listed on the main menu for members only. Each of those two pages is protected with a password.

    When a site visitor clicks on one of those password protected links, is there a piece of PHP code I can add to the sites’s child theme that would prevent the sidebar associated with those private pages from being displayed until AFTER the password is entered?

    Very good WP theme, by the way.

    Thanks!

    Peter

    GP Premium 1.2.96
    #306325
    Tom
    Lead Developer
    Lead Developer

    Hi Peter,

    You could use this filter:

    So basically, set all sidebars to “No Sidebar” for users who aren’t logged in: https://docs.generatepress.com/article/sidebar-layout/

    add_filter( 'generate_sidebar_layout','tu_logged_out_sidebar' );
    function tu_logged_out_sidebar( $layout )
    {
     	if ( ! is_user_logged_in() ) {
                return 'no-sidebar';
            }
    
     	// Or else, set the regular layout
     	return $layout;
     }

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    Otherwise, you could display different widgets to non-logged in users using a plugin like this: https://en-ca.wordpress.org/plugins/content-aware-sidebars/

    #307043
    Peter

    Hi Tom –

    Does one simply copy all of the above lines of code and paste them into the Child Theme’s functions.php file? I tried doing that, and being a novice to unforgiving PHP, it returned a blank page.

    The other PHP lines of code in my site’s Child Theme look like they have slightly different syntax. They start with function and end with add_filter. For example, there are just two functions in the Child Theme, and this is the second one, ending with a semi-colon:

    // password cookie time-out after session.
    function custom_password_cookie_expiry( $expires ) {
    return 0; // Make it a session cookie
    }
    add_filter( ‘post_password_expires’, ‘custom_password_cookie_expiry’ );

    #307095
    Tom
    Lead Developer
    Lead Developer
    #307101
    Peter

    Yes, I followed the instructions under Use a Child Theme.

    When I copied the generate_sidebar code (add-filter, etc, above) and pasted it into the functions.php file in the child theme folder, the site would not load.

    Odd.

    Thanks, Tom.

    #307104
    Tom
    Lead Developer
    Lead Developer

    It’s working inside code snippets?

    Odd about functions.php – must be some conflicting php going on.

    #307133
    Peter

    Found out why it was crashing. I used the code that came by way of email (not the box above). It was corrupted, but being a newbie, I didn’t notice.

    The code now hides the sidebar when the member password window opens.

    However, after the password is entered, the page shows up, but the sidebar does not load, and remains hidden.

    By the way, there are two pages on the site which require passwords to visit. Each has a different password.

    I’ll try a plugin, but I’d like to keep the use of plugins to a minimum.

    Thanks
    Peter

    #307354
    Tom
    Lead Developer
    Lead Developer

    That code will return the default setting if the user is logged in. So it will either return the specific page sidebar layout, or the global layout set in Customize > Layout > Sidebars.

    #307391
    Peter

    Still no luck with the sidebar php code.

    To review, two password protected Pages have a sidebar containing topic headings for the site’s Notice Board & Forum.

    Normally, when password protected Pages are clicked in the menu bar, an entry box appears with “This content is password protected. To view it please enter your password below:” However, while the content of the Page is not displayed, the associated sidebar displays all the topics in the Notice Board & Forum. That Sidebar and Widget needs to be hidden until the password is entered.

    Notice Board & Forum entries listed in the sidebar widget are private or public, depending on the purpose of the notice. If the notice is public (no password protection), it is also displayed on an public events Page which uses the WP Show Posts plugin.

    Page layout settings:

    In Customize > Sidebars, all three sidebar layout settings are set to Content / Sidebar.

    On the Pages with password protection, the sidebar layout is set to Global Layout Settings. Visibility is set to Password protected.

    BTW, I have been using Dreamweaver CS3 to edit the PHP files, and interact with the server. This has been working thus far for other projects.

    Thanks, Tom

    #307455
    Tom
    Lead Developer
    Lead Developer

    Any chance you can send me temporary admin login details so I can take a look?: https://generatepress.com/contact/

    #307783
    Peter

    Yes. Emailed the creds to you yesterday.

    Thanks

    #309023
    Tom
    Lead Developer
    Lead Developer

    Ah, we’re not checking to see if the user is logged in, we’re checking to see if they’ve entered the password correctly.

    Try this:

    add_filter( 'generate_sidebar_layout','tu_logged_out_sidebar' );
    function tu_logged_out_sidebar( $layout )
    {
     	if ( post_password_required() ) {
                return 'no-sidebar';
            }
    
     	// Or else, set the regular layout
     	return $layout;
     }
    #309678
    Peter

    Aha. Exactly. The post_password_required code does the job! Thank you Tom.
    – Peter

    #309699
    Tom
    Lead Developer
    Lead Developer
Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.