How to redirect user after login wordPress? Sometimes, you need to redirect user after login in WordPress. But how and why should you even need to do so?! Here is an example: suppose you need to redirect any users with customer role to your shop home page, and this definitely leads to a better user experience.
In another case, you may need to redirect user that has login in WordPress to an online assessment form.
Any way! You need to redirect user, and here we are to show you how to redirect user after login in WordPress. You can learn how to redirect user by role, username, or whatever you want by reading this article. As usual, there are two ways. You can redirect users after logging in WordPress with a plugin or without a plugin. We cover Both procedures in this article of wpexpelor.
Redirect user after login WordPress with plugin
To redirect user after login in WordPress with plugin, fallow the steps:
- Click on the plugin parts to access the WordPress repository.
- Search “LoginWP” and install and activate the plugin.
- As we have shown in the picture, from the dashboard, go to the “Redirections” part.
- From “Redirection Rules” part, click the add button.
- From “Rule Condition” part, you can define different rules based on username, role, etc., and you can even define new rules.
How do the rules work here? Suppose we have defined a rule for administrative roles in our system. The “Order” field shows the importance of the situation. Number 1 has the most importance, and as the number decreases, the priority increases.
From “Redirect URLs”you can set which user roles would be redirected to which pages. This trick can be implemented even after user log out of your site. (redirect user after logout in WordPress).
For example, we have defined admin user should be redirected to the shop home page after login, and he should be redirected to the “cart” after logging out!
Eventually, save the role that you have defined.
Redirect user after login WordPress without plugin
There are so many reasons that people don’t want to use WordPress plugins to get the job done! You may have another plugin that interferes with this one. So in wpexpelor we try to provide you with both ways.
To Redirect user after login in WordPress without plugin copy and paste this code in to the functions.php file in your WordPress theme.
Note! For consistent changing, use child theme.
This code will implement your rule for all user roles. In fact, all user will be redirected to the same page after login in WordPress.
function custom_redirect_after_login() {
if (is_user_logged_in()) {
wp_redirect('https://example.com/your-desired-page/');
exit;
}
}
add_action('template_redirect', 'custom_redirect_after_login');
If you want to refactor the code based on user role, here is the result:
function custom_redirect_based_on_role() {
if (is_user_logged_in()) {
$user = wp_get_current_user();
if (in_array('subscriber', $user->roles)) {
wp_redirect('https://example.com/subscriber-page/');
} elseif (in_array('customer', $user->roles)) {
wp_redirect('https://example.com/customer-page/');
} elseif (in_array('administrator', $user->roles)) {
wp_redirect('https://example.com/admin-page/');
} else {
wp_redirect('https://example.com/default-page/');
}
exit;
}
}
add_action('template_redirect', 'custom_redirect_based_on_role');
As you see in this code, we have separated the roles and target page.
Note: replace your desired page URL with our test addresses!
feel free to ask any questions in comments.Hope the article is useful for you.
1 Comment
Thank you for sharing your knowledge on how to redirect users after login in WordPress. I always struggle with figuring out the best way to do this, and your blog post has given me some great ideas. I especially appreciate your step-by-step instructions and the mention of using a plugin – that will definitely save me time and effort in the future. Keep up the great work!