Change WordPress user roles and capabilities › Forums › How to or FAQ › Block front-end options › Reply To: Block front-end options
22/01/2015 at 02:19
#1367
Vladimir
Keymaster
In order to have a role, a user should login 1st. Not logged in user can not have any role.
As home page is available to the public I offer you replace those links for not logged in users with the registration/login links.
And add a filter for automatic redirection after login to the restricted page according to the assigned role. Like this:
add_filter('login_redirect', 'redirect_user_after_login', 10, 3);
function redirect_user_after_login($redirect_to, $request, $user) {
if (isset( $user->allcaps ) && is_array( $user->allcaps )) {
//check for admins
if (!empty($user->allcaps['administrator'])) {
// redirect them to the default place
return $redirect_to;
} else if (!empty($user->allcaps['candidate'])) {
$redirect_to = home_url() .'/post-resume';
} else if (!empty($user->allcaps['employer'])) {
$redirect_to = home_url() .'/post-job';
} else {
return home_url();
}
} else {
return $redirect_to;
}
}