WordPress sites that offer personalized or private content typically want to keep their users on the front end of the site. However, WordPress automatically takes users to the back end dashboard after they login. Obviously, we need to be able to redirect these users to the front end of the site; but at the same time, anyone who is an admin will probably still want to end up on the back end of the site. WordPress gives us the ability to edit where users are sent after they login by using the
The code snippet below will allow you to easily detect the user’s role and redirect them to the appropriate location:
Note that the
has_cap() method in the code above will check if a user has a specific role or capability. Additionally, the
home_url() function will take a slug or relative path and can also be replaced with
get_permalink() if you prefer to pass in a specific page or post id.
Another possibility is to redirect a WordPress user on login based on custom user meta:
One great possible use of the code above would be localization. For example, lets say that you have a restaurant with five locations. When a user logs in to your website, they can place their lunch order at their favorite location. If you were to simply store their favorite location as custom user meta, then you could check that data when they login and automatically send them to the correct order page.