I recently had a WordPress problem. I used a plugin to redirect user seeing private page to the login page. However, subscribers can’t see private post once logged in. So it was creating a redirect loop. You can change that with a few lines of code.
How to allow subscriber to view private page on your website
In your theme folder, in functions.php add the following lines:
<?php
// Allow subscribers to see Private posts and pages
$subRole = get_role( 'subscriber' );
$subRole->add_cap( 'read_private_posts' );
$subRole->add_cap( 'read_private_pages' );
Add the next lines to redirect subscribers to the homepage once logged in
// Redirect to home page on login
function loginRedirect( $redirect_to, $request_redirect_to, $user ) {
if ( is_a( $user, 'WP_User' ) && $user->has_cap( 'edit_posts' ) === false ) {
return get_bloginfo( 'siteurl' );
}
return $redirect_to; }
add_filter( 'login_redirect', 'loginRedirect', 10, 3 );
Tada.
Thank you this worked for me with a Divi child theme. great work.
This worked great for me in Divi.
How would I extend this to include authors as well as subscribers, but still allow authors to post?
Would this work?
$subRole = get_role( ‘author’ );
$subRole->add_cap( ‘read_private_posts’ );
$subRole->add_cap( ‘read_private_pages’ );
// Redirect to home page on login
function loginRedirect( $redirect_to, $request_redirect_to, $user ) {
if ( is_a( $user, ‘WP_User’ ) && $user->has_cap( ‘edit_posts’ ) === true ) {
return get_bloginfo( ‘siteurl’ );
}
return $redirect_to; }
add_filter( ‘login_redirect’, ‘loginRedirect’, 10, 3 );
Thanks Best Nick