Redirect Users After Login by Role in WordPress

Home / Code Scripts / Redirect Users After Login by Role in WordPress

Resource Type

Code Scripts

Complexity Level

Intermediate

Last Updated

July 29, 2025

Description

This PHP snippet lets you redirect users to different pages based on their role immediately after they log in. Great for membership sites, e-commerce dashboards, or multi-user WordPress setups.

PHP

				
					/**
 * Redirect Users After Login Based on Role
 */
function rcwebkit_login_redirect( $redirect_to, $request, $user ) {
    // Check if user object exists
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        if ( in_array( 'administrator', $user->roles ) ) {
            // Redirect admins to dashboard
            return admin_url();
        } elseif ( in_array( 'editor', $user->roles ) ) {
            // Redirect editors to posts page
            return admin_url( 'edit.php' );
        } elseif ( in_array( 'customer', $user->roles ) ) {
            // Redirect WooCommerce customers to shop
            return home_url( '/shop' );
        } else {
            // Default redirect for all other roles
            return home_url( '/account' );
        }
    } else {
        return $redirect_to;
    }
}
add_filter( 'login_redirect', 'rcwebkit_login_redirect', 10, 3 );

				
			

Key Features:

  • Redirect based on role (administrator, editor, customer, etc.)

  • No plugin required

  • Easy to customize

 

Steps to Use:

  1. Edit the URLs in the code (e.g., /shop, /account) as per your site structure.

  2. Paste this snippet into your theme’s functions.php file or a site-specific plugin.

  3. Log out and log in with different roles to test.

Search

Connect with us

Blog Categories

recent posts

Scroll to Top