- This topic has 13 replies, 2 voices, and was last updated 7 years ago by Tom.
-
AuthorPosts
-
April 15, 2017 at 10:02 am #306320Peter
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
April 15, 2017 at 10:37 am #306325TomLead DeveloperLead DeveloperHi 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/
April 17, 2017 at 5:16 pm #307043PeterHi 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’ );April 17, 2017 at 9:01 pm #307095TomLead DeveloperLead DeveloperI would try it with these instructions: https://docs.generatepress.com/article/adding-php/
April 17, 2017 at 9:26 pm #307101PeterYes, 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.
April 17, 2017 at 10:11 pm #307104TomLead DeveloperLead DeveloperIt’s working inside code snippets?
Odd about functions.php – must be some conflicting php going on.
April 18, 2017 at 12:34 am #307133PeterFound 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
PeterApril 18, 2017 at 9:25 am #307354TomLead DeveloperLead DeveloperThat 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.
April 18, 2017 at 11:05 am #307391PeterStill 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
April 18, 2017 at 1:19 pm #307455TomLead DeveloperLead DeveloperAny chance you can send me temporary admin login details so I can take a look?: https://generatepress.com/contact/
April 19, 2017 at 10:35 am #307783PeterYes. Emailed the creds to you yesterday.
Thanks
April 22, 2017 at 11:03 am #309023TomLead DeveloperLead DeveloperAh, 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; }
April 24, 2017 at 9:01 am #309678PeterAha. Exactly. The post_password_required code does the job! Thank you Tom.
– PeterApril 24, 2017 at 10:09 am #309699TomLead DeveloperLead DeveloperYou’re welcome 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.