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:
- login_redirect – Redirect user once they login
- after_setup_theme – Remove admin bar
- admin_init – Keeps subscribers out of admin panel
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.