Home > Dev > WordPress Customization > Redirecting Users After Login Based on Role

Redirecting Users After Login Based on Role

This code will redirect users to different pages based on their role after login.

// Add this to your theme's functions.php file

function redirect_users_by_role() {
    $user = wp_get_current_user();
    if (in_array('administrator', $user->roles)) {
        wp_redirect(admin_url());
        exit;
    } elseif (in_array('editor', $user->roles)) {
        wp_redirect(site_url('/editor-dashboard'));
        exit;
    } elseif (in_array('subscriber', $user->roles)) {
        wp_redirect(site_url('/subscriber-dashboard'));
        exit;
    }
}
add_action('wp_login', 'redirect_users_by_role');
Back