Home » After Login Redirection By User Roles In WordPress

After Login Redirection By User Roles In WordPress

Last updated on June 16, 2022 by

Are you using login and registration with multiple user roles? Do you want to after Login redirection by User Roles? If yes, then we have a very very simple solution for you. In this article, we will discuss how to redirect users after they login by user roles in WordPress.

Normally, We don’t suggest installing plugins for smaller tasks because it may impact the performance and security of the site. So we are doing this via simple code. Are you ready? Let’s dive into it.

Table of Contents
1. An Ideal Place to Add Custom Code in WordPress
2. What Are We Going To Do In Very Simple Words?
3. Setting up Login Redirect via Code in WordPress

Now, to accomplish WordPress after login redirect by user roles, we will see some extra stuff like where to add code in WordPress & What will do by taking some examples. Let’s see how to do it.

01 An Ideal Place to Add Custom Code in WordPress

Most Important: Add the following code to your child theme’s functions.php file. If you add custom code directly to your parent theme’s functions.php file then it will be wiped entirely when you update the theme.

If you are using a custom theme and if it doesn’t require any update then you can directly place the code into wp-content/themes/your-theme/function.php file.

Please also note that our team has tested all codes with the GeneratePress child theme & Storefront child theme.

02 What Are We Going To Do In Very Simple Words?

Here, the filter hook login_redirect is used to redirect users. With the help of this hook, we will modify the default redirection link and wrap it up with the user role condition.

Does it make sense to you? It sounds very very simple. Isn’t it? Let’s go ahead and do that.

03 After Login Redirect Programmatically In WordPress

We are presuming that your website has 3 different roles for the front-end side like ‘teacher’, ‘student’, and ‘super-user’.

Now, again we are presuming that you want to redirect the ‘student’ role to the Daily Class page(ID:77), the ‘teacher’ role to the Attendance page(ID:78), and the ‘super-user’ role to the Activity page(ID:79).

So add the below code into your child theme’s function.php file for the WordPress after login redirect as shown below:

/*@ After login redirection by user role */
if ( !function_exists('tf_after_login_redirection_by_user_roles') ):

	add_filter( 'login_redirect', 'tf_after_login_redirection_by_user_roles', 10, 3 );	
	function tf_after_login_redirection_by_user_roles( $redirect_to, $request, $user ) {
	    global $user;

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

	        if ( in_array( 'student', $user->roles ) ) :

	        	$page_id = 77;
	            return get_permalink($page_id);

	        elseif ( in_array( 'teacher', $user->roles ) ) :

	        	$page_id = 78;
	            return get_permalink($page_id);

	        elseif ( in_array( 'super-user', $user->roles ) ) :

	        	$page_id = 79;
	            return get_permalink($page_id);

	            return home_url();
	        return $redirect_to;

In the above example, You just need to replace your roles and page IDs. You can also add or remove IF conditions as per your requirement.

Hurray! we have just completed the minimum steps for WordPress after login redirect by user roles.

Additionally, read our guide:

  1. How To Add Back Button In Elementor
  2. 403 Error When Updating In Elementor
  3. How To Add Multiple Post Types In Posts Widget In Elementor
  4. How to Add Products Per Page Dropdown in WooCommerce
  5. “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
  6. How to Create a Plugin in WordPress from Scratch
  7. How to Disable Admin Bar in WordPress Without Plugin
  8. How To Send Custom Emails in WordPress
  9. How to Allow Preview of Draft Post Without Login in WordPress
  10. Import Users From CSV In WordPress Programmatically
  11. Dynamically Populate A Select Field’s Choices In ACF
  12. How To Create Database Table In WordPress
  13. Difference Of require, include, require_once And include_once
  14. PHP: Get Day Name From Date
  15. PHP: Get Year From Date
  16. How To Update Pivot Table In Laravel
  17. Laravel 9 Resource Controller And Route With Example
  18. Laravel 9 File Upload Tutorial With Example
  19. How To Schedule Tasks In Laravel With Example
  20. Laravel Collection Push() And Put() With Example

That’s it for now. We hope this article helped you to learn after login redirection by user roles in WordPress.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!


Leave a Comment