This will get you started on the right track.
Call wp_get_current_user()
whose return value will be either one of a WP_User
object or null
value. Assuming we have a WP_User
object, we can inspect the roles
property found on the object and conditionally check if it matches an allowed role prior to redirecting the user.
function redirect_user() {
$user = wp_get_current_user();
if ( ! is_null($user) && in_array('vendor', $user->roles) ) {
//do wp_redirect() or wp_safe_redirect()
} else {
//do something else
}
}
add_action('template_redirect', 'redirect_user');
You can also use is_user_logged_in()
as part of your conditional logic to check whether a user is logged in or not – if you prefer.