1

We are building a WordPress site where one of the pages needs to be accessed via username and password.

I know that I can create a "Private" page, where the user can enter a password to view. However, our client has asked us for the ability to create unique username/password combinations for this particular page. Because of this, I'd also like to build in the ability to reset a forgotten password.

I've researched online but haven't found any viable solutions. I'm wondering if there's a best practice in place for this. I'm thinking that perhaps we create a new user role and hide the WordPress dashboard for this type of user? Am I on the right track? Any experiences would be greatly appreciated.

  • when you say "password" do you mean the users password or the pages password? – Howdy_McGee Jun 4 '15 at 17:34
  • Thanks for the clarification -- I meant the user's password. – shackleton Jun 4 '15 at 17:59
  • So really, the only prerequisite is the user has to be logged in to view the private page? – Howdy_McGee Jun 4 '15 at 18:00
  • Correct. Logged in to view the page, but we don't want to give them access to the wordpress dashboard. Upon login, they'll go directly to the private page, instead of the dashboard – shackleton Jun 4 '15 at 19:46
1

So here's what we're trying to do:

IF A user logs in and is of a specific role ( Subscriber ) then redirect them to a specific page.

IF A user tries to reach the admin panel, redirect them to a specific page.

We're going to use 3 hooks for the above problems:

First, let's create our redirect:

/**
 * Redirect subscribers to specific page
 * @param string $redirect
 * @param string $request
 * return string
 */
function member_redirect( $redirect, $request ) {
    if( current_user_can( 'subscriber' ) ) {
        return $specific_page
    }

    return $redirect;
}
add_filter( 'login_redirect', 'member_redirect', 10, 2 );

If the current user is a subscriber, we would replace the $specific_page with the page URL, otherwise let them go to where they were originally headed.

Next, let's make sure subscribers can't get to the admin panel:

/**
 * If user is subscriber AND trying to access admin panel, redirect them to specific page
 */
function subs_adminpanel_redirect(){
    if ( current_user_can( 'subscriber' ) ){
        wp_redirect( $specific_page );
        exit;
    }
}
add_action( 'admin_init', 'subs_adminpanel_redirect' );

Again, you could replace $specific_page with your actual URL.

Finally, let's remove the admin bar so the user doesn't even know there's a dashboard.

/**
 * Remove adminbar for Subscribers
 */
function subs_remove_adminbar() { 
    if( ! current_user_can( 'subscriber' ) ) {
        add_filter( 'show_admin_bar', '__return_false' );   
    }
}
add_action( 'after_setup_theme', 'subs_remove_adminbar' );

This is pretty straight forward, if they're a subscriber remove the admin bar for them.

A step shackleton pointed out in the comments below is by default, subscribers do not have access to view private pages. We need to add this as a capability:

/**
 * Allow Subscribers to view Private Pages
 */
function add_theme_caps() {
    $role = get_role( 'subscriber' );
    $role->add_cap( 'read_private_pages' );
}
add_action( 'admin_init', 'add_theme_caps' );

You would throw all this into your functions.php file at some place and test it out. Again, this only works for subscribers and only if you replace $specific_page with your actual page URL.

  • 1
    This is fantastic. Thanks so much for the thoughtful reply. I ran into 2 issues, which you may want to edit above 1) in the 2nd and 3rd functions above, remove the ! before current_user_can, and 2) subscribers cannot view private pages, so I added (comments wont allow line breaks, so posting to next comment): – shackleton Jun 5 '15 at 1:36
  • /** * Allow Subscribers to View Private Pages */ function add_theme_caps() { $role = get_role( 'subscriber' ); $role->add_cap( 'read_private_pages' ); } add_action( 'admin_init', 'add_theme_caps'); – shackleton Jun 5 '15 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.