Setting the Visibility of a Page or Post to 'Password protected' in WordPress will hide the content until the Password is entered by a user.
A member of GenesisWP Facebook group wrote:
I password protected some pages in my site (I'm using a customized version of Genesis Sample theme), the problem is that the sidebar is still visible.
The sidebar only displays on those pages, the rest of my pages are full widht, so the widgets I need in my sidebar are for the password protected pages only.
We can use post_password_required() function to check whether a single entry requires password and the correct password has been provided. Using this conditional in combination with actions in Genesis, Primary sidebar can be removed until the password has been entered.
Before entering password:
After entering password:
To not show the Primary Sidebar until the password has been entered, add the following in child theme's functions.php:
To view the full content, please sign up for the membership.
Already a member? Log in below or here.
Is it possible to hide the sidebar as you have done and also then move the sidebar before content after logging into a post (as opposed to a page)?
The following code works in isolation (to move the sidebar above content on posts [specific category]) but cancels out the hidden sidebar code. I am able to implement on pages so I think it’s to do with the order in which the actions fire. Not sure.
add_action(‘wp’, ‘ko_move_sidebar_before_content_da’);
function ko_move_sidebar_before_content_da() {
if(!in_category(‘da’))
return;
remove_action( ‘genesis_after_content’, ‘genesis_get_sidebar’ );
add_action( ‘genesis_before_content’, ‘genesis_get_sidebar’ );
}
OK so was able to sort this by using the WordPress action hook wp_login – http://wordpress.stackexchange.com/questions/28522/is-there-a-hook-that-runs-after-a-user-logs-in
I think there is a more concise way of doing it (and the problem would reappear if I wanted to move the sidebar on unprotected posts) but the following hides the sidebar on protected posts AND moves sidebar before content once “logged in” to post.
add_action( ‘genesis_after_loop’, ‘sk_primary_sidebar_visibility’ );
/**
* Remove the Primary sidebar from appearing on Password protected entries.
*
* @author Sridhar Katakam
* @link http://sridharkatakam.com/
*/
function sk_primary_sidebar_visibility() {
if ( post_password_required() ) {
remove_action( ‘genesis_after_content’, ‘genesis_get_sidebar’ );
}
}
add_action(‘wp_login’, ‘ko_move_sidebar_before_content_da’);
function ko_move_sidebar_before_content_da() {
if(!in_category(‘da’))
return;
remove_action( ‘genesis_after_content’, ‘genesis_get_sidebar’ );
add_action( ‘genesis_before_content’, ‘genesis_get_sidebar’ );
}
Spoke too soon. The sidebar isn’t repositioned so don’t think the second action is doing anything.
Ended up going with the CSS option for specific posts to avoid conflict (added da as custom body class on those posts)
@media only screen and (max-width: 1023px) {
.da .content-sidebar-wrap {display:table;}
.da .content {display:table-footer-group; float: none;}
.da .sidebar {display:table-header-group; float: none;}
}