How to Redirect Users Based on Role in WordPress After Login

WordPress is a popular and robust content management system that enables the management of user roles and capabilities. A common need for many websites is the ability to direct users to different pages depending on their assigned roles.

WordPress provides a range of flexible options to direct administrators to a specific dashboard, subscribers to a members-only area, or redirect users to a custom landing page based on their roles.

In this article, we will explore various methods to redirect users based on their roles in WordPress. By the end of this article, you will better understand the different approaches you can take to redirect users based on their assigned roles, and you will be able to implement these methods into your WordPress site easily.

2 Simplest Methods to Redirect WordPress Users After Login

Method 1: Using a Plugin

WordPress has a vast repository of plugins that can simplify complex tasks. One popular plugin that enables role-based redirection is “Peter’s Login Redirect.”

Here’s a Step-by-step Guide to redirecting after login in WordPress Site:

  1. First, go to your WordPress Dashboard.
  1. Navigate to the “Plugin” section, and click the “Add New” button.
Add New Pugin
  1. Search for “Peter’s Login Redirect” and “Activate” the plugin.
  1. After activating the plugin, click the “LoginWP” option in the left-hand menu and click the “Redirections” option.
Open the Redirections Option

Set Up Login Redirect for Specific WordPress Users

  1. After clicking the “Redirections” option, you will show the menu of the “Redirection Rules” options, and then click the “Add New” option.
redirect users based on role
  1. This brings you to a “New Page” to set your redirection settings.
redirect users based on role
  1. Select the “Username” criteria from the “Rule criteria” drop-down and enter the username.
redirect users based on role
  1. Then, on login and logout, you may specify the URLs to which you wish to redirect the user to save your changes and click the “Save Rule” button.
redirect users based on role

Configuring WordPress’s login redirect by user role

The steps above will apply to the procedure. Just select “Add New” from the “Redirection Rules” section’s buttons. Next, choose the user role from the drop-down list after selecting the “User Role” condition from the “Rule Condition” drop-down.

Additionally, you may change the order to any number, which will affect how the plugin settings will save and show this rule.

redirect users based on role

After that, provide the login and logout URLs for the locations where you want to route the user role. After that, press the “Save Rule” button.

redirection rules

Simply repeat the aforementioned procedures to establish various login redirections for various user roles.

WordPress Login Redirect Configuration for All Users

Setting a redirect for every other user is an option on the plugin’s options page. You can redirect users by providing a URL here if they don’t meet any of the restrictions you’ve put up above.

The “All Other Users” section needs to have a login URL and a logout URL. Then, click the “Save Changes” button.

redirect users based on role

Save your settings; the plugin will redirect users according to their assigned roles.

🔥PLAY SAFE: With WP Staging, you can test updates, plugins, and themes before going live, ensuring a seamless user experience. GRAB THE OFFER!

Method 2: Custom Code Snippet

If you prefer a more hands-on approach, you can achieve role-based redirection by adding a custom code snippet to your WordPress theme’s functions.php file. Follow these steps to use this plugin:

  1. Access your WordPress installation via FTP or a file manager from your hosting control panel.
redirect users based on role
  1. Navigate to the wp-content directory containing your themes and plugins.
redirect users based on role
  1. Open the “themes” folder and locate the folder corresponding to your active theme. This folder’s name should match the theme you have currently activated on your WordPress website.
  1. Search for the file named functions.php within your active theme’s folder. This file handles various functions and customizations within your WordPress theme.
  1. Open the functions.php file using a text editor and add the following code snippet:
PHP
function role_based_redirect() {
    if ( is_user_logged_in() ) {
        $user = wp_get_current_user();
        $roles = ( array ) $user->roles;
        if ( in_array( 'administrator', $roles ) ) {
            wp_redirect( 'https://example.com/admin-dashboard' );
            exit;
        } elseif ( in_array( 'subscriber', $roles ) ) {
            wp_redirect( 'https://example.com/members-area' );
            exit;
        } // Add more conditions for other roles and URLs as needed
    }
}
add_action( 'template_redirect', 'role_based_redirect' );
  1. Modify the URLs in the wp_redirect() function to the desired destination URLs for each role. Save the functions.php file, and the redirection rules will take effect immediately.

Conclusion

Redirecting users based on their roles is a valuable feature that can significantly enhance the user experience by providing targeted content to specific user groups. WordPress offers flexibility in achieving role-based redirection, whether you opt for a plugin or implement a custom code snippet.

PRO TIP: For a worry-free website development experience, WP Staging is a trusted tool for creating staging environments in WordPress. YOU CAN DOWNLOAD IT FOR FREE BY CLICKING ON THIS LINK!

Related Articles: