A Cozmoslabs Product

Redirect based on username

This function checks a user username (user_login), after that username is send in a switch case and redirect user to a given link based on username.

Make sure to:

  • change username on line 13, 16, 19 and 22
  • change redirect link in $redirect_to variable on line 14, 17, 20, 23 and 26

login_redirect filter is used to change the location redirected to after logging in and is located in wp-login.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * Redirect based on username
 */
 
add_filter( 'login_redirect', 'wppbc_custom_login_redirect', 99, 3 );
function wppbc_custom_login_redirect( $redirect_to, $requested_redirect_to, $user ) {
   if( is_wp_error( $user ) ) {
      return $redirect_to;
   }
   $username = $user->user_login;
   if ( is_array( $form_args ) ) {
      switch ( $username ) {
         case 'admin':
            $redirect_to = 'http://www.website.com/page1';
            break;
         case 'username1':
            $redirect_to = 'http://www.website.com/page2';
            break;
         case 'username2':
            $redirect_to = 'http://www.website.com/page3';
            break;
         case 'username3':
            $redirect_to = 'http://www.website.com/page4';
            break;
         default:
            $redirect_to = 'http://www.website.com/default-redirect-for-other-users';
      }
   }
   return $redirect_to;
}

The Ultimate Membership Bundle

Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.

Get 25% off with the bundle

Help & Support

We’re here to help you every step of the way.

Open a Support Ticket