This is a topic that seems to pop-up a lot for WordPress plugins that offers user management features or something similar. A lot of support threads consist of questions similar to “How can I redirect users after login for my WordPress website” or “Can I redirect user’s according to their WordPress user role”. One great thing about WordPress is it’s vast range of free plugins that are absolutely amazing.
In this blog post, you will find out about some of the plugins that easily do this for you and a simple code example which should be a great starter point if you are wanting to get into development. Here is a list of some of our recommended WordPress plugins that will redirect users after login:
Theme My Login (TML)
Theme My Login is my most preferred plugin as this offers more than just a simple redirect after login feature. Some of it’s features include front-end registration, login and disable the dashboard for users with certain roles. Our site makes use of this plugin for some of these features, this is ideal if you want to hide the fact that your site is a WordPress based website to your users.
Redirect After Login
This plugin says what it does. You can create custom page redirects after a user logs into your WordPress website and is ideal if this is the only feature you would need.
Peter’s Login Redirect
Similar to Redirect After Login, Peter’s Login Redirect does what it says and is another alternative to the previous plugins mentioned above.
Something For The Developers
So I’ve gotten to a point in my career where I love to code, as you can tell by the many plugins we’ve released and still plan to release, and I absolutely love to figure out how other developers would tackle scenarios and learn from that. Something as simple as to redirect users after login might be easy for some and a mountain for others – I’m more than happy to share and explain code.
This code may seem intimidating for some developers but once you get the hang of WordPress hooks and functions, it will seem rather simple. The $user object
is passed in through the login_redirect
filter which means the current user’s data that is logging in will be available. This means that you will be able to redirect user’s after login not only by user role but by any other data linked to that user’s account, such as user_meta
.
The additional checks of is_array
and isset
is just to ensure that the data exists before trying to see if the user is not an administrator and this is done on line 12
of the gist, by checking to see if the user role is not administrator
.
Some of the code was taken from the login redirect documentation, an interesting note is that the $current_user global
is not available on this filter and you may need to rather use the $user object
that is passed in the filter or the $user global
instead.
Where To Now?
I hope this blog was helpful and the code is clearly understandable for developers of all levels, including developers just starting out. As you may know there are many ways to achieve the same result and you may be interested in looking into the wp_login
filter. If you are looking to code, why don’t you try redirect user’s after login and hook into When Last Login’s custom login action called wll_logged_in_action
which can be found over here: https://github.com/andrewlimaza/when-last-login/blob/master/when-last-login.php#L300