When users log into a WordPress site the default action is to redirect them to the WordPress admin dashboard. Redirections WordPress is fine if you are the site admin or the user needs access to the admin but what about when you don’t want your site’s users to be able to access the admin dashboard. WordPress core does not provide a way to redirect users.

Redirection after Registration

Normally the users are redirected to the WordPress login after successful registration. Now let us suppose you have a special offer on your site to attract newly registered users and you want to redirect them to that particular page. You can achieve this by simply writing below code snippet in your active theme functions.php file.

   add_filter( 'registration_redirect', 'my_redirect_home' );

   function my_redirect_home( $registration_redirect ) {

   	return home_url();

   }

The registrations wordpress redirect filter is used to change the location redirected to after a user register. This could be the location set by the “redirect_to” parameter sent to the registration page.This simple example will redirect a user to the home_url() upon successful registration.

You can redirect to the desired page like below

   add_filter( 'registration_redirect', 'redirect_after_registration' ); 

   function redirect_after_registration() {

       // change the slug below with desired page slug

       return home_url( '/new-user-registration/' ); 

   }

Redirection after Login

After login, users are sent to the dashboard. If site users don’t need to see the dashboard for some reason, you can keep them in the front-end by writing following code in functions.php file.

   add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

   function my_login_redirect( $redirect_to, $request, $user ) {

   	//is there a user to check?

   	if ( isset( $user->roles ) && is_array( $user->roles ) ) {

   		//check for admins

   		if ( in_array( 'administrator', $user->roles ) ) {

   			// redirect them to the default place

   			return $redirect_to;

   		} else {

   			return home_url();

   		}

   	} else {

   		return $redirect_to;

   	}

   }

The login_redirect filter is used to change the location redirected to after logging in. This could be the location set by the “redirect_to” parameter sent to the login page. This example redirects admins to the dashboard and other users to the homepage. Make sure you use add_filter outside of is_admin(), since that function is not available when the filter is called.

Redirection after Logout

To redirect users after logging out.Use following code snippet in your active theme functions.php file.

   add_filter( 'logout_redirect', 'user_logout_page', 10, 3 );

   function goodbye_subscribers( $redirect_to, $request, $user ) {

       global $user;

       if ( isset( $user->roles ) && is_array( $user->roles ) ) {

           if ( in_array( 'subscriber', $user->roles ) ) {

               // change the slug below with desired page slug

               return home_url( '/user-logout/' );

           } else {

               return $redirect_to;

               }

       }

   return;

   }

Redirection upon Password Reset

The lostpassword_redirect filter is used to change the location redirected to after a user requests a password reset. This could be the location set by the “redirect_to” parameter sent to the forgot password page.
This simple example will redirect a user to the home_url() upon successful password reset.

   add_filter( 'lostpassword_redirect', 'my_redirect_home' );

   function my_redirect_home( $lostpassword_redirect ) {

   	return home_url();

   }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Wbcom Designs