When your website means business.

Custom Admin, Users | Type: PHPRedirect Users on Login Based on Role

This plugin allows redirection of users when they login, based on role.

<?php
/*
Plugin Name: Redirect Users by Role
Plugin URI: https://evowebdev.com
Description: Redirects users based on role
Version: 1.0
Author: Evo Web Dev, Ray Gulick
Author URI: https://evowebdev.com
License: GPLv2 or later

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

function evo_redirect_users_by_role() {
    if ( ! defined( 'DOING_AJAX' ) ) {
        $current_user = wp_get_current_user();
        $role_name = $current_user->roles[0];
        if ( 'subscriber' === $role_name ) {
            wp_redirect( 'http://yoursite.com/dashboard' );
        }
    }
}
add_action( 'admin_init', 'evo_redirect_users_by_role' );
?>

Another approach, in functions.php:

function conditional_login_redirect( $redirect_to, $requested_redirect_to, $user ) {
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        if ( in_array( 'subscriber', $user->roles ) ) {
            return home_url(); // subscriber role redirected to homepage
        } else {
            return $redirect_to; // other roles to the original URL
        }
    }
    return $redirect_to;
}
add_filter( 'login_redirect', 'conditional_login_redirect', 10, 3 );

Menu
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Cookies Notice