Site icon Denis Bouquet

Allow subscriber to see private page in WordPress

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.

Exit mobile version