Redirecting Users After Login Like a Pro 🚀
Hey there, fellow WordPress wrangler! Ever wanted to automatically shuffle your users to a specific page after they log in, like a bouncer sending people to the right VIP lounge? Well, here’s how you can do it, and trust me, it’s easier than finding the right GIF for a group chat.
Method 1: Code Magic in functions.php
or a Custom Plugin
You can add the following snippet into your theme’s trusty functions.php
file or even better, your custom plugin (because who doesn't love having their own plugin?).
function developernoob_login_redirect( $redirect_to, $request, $user ) {
// Is this user logged in and part of the cool crowd?
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
// If they're a 'subscriber', send them to their personal dashboard (fancy, right?)
if ( in_array( 'subscriber', $user->roles ) ) {
return home_url( '/subscriber-dashboard/' ); // You get a dashboard, and you get a dashboard!
}
// Admins go straight to the admin panel to do admin-y things
elseif ( in_array( 'administrator', $user->roles ) ) {
return admin_url(); // Because where else would they want to go?
}
// All other folks? Let's give them a generic dashboard, we don't discriminate here.
else {
return home_url( '/default-dashboard/' ); // The one-size-fits-all dashboard!
}
}
// If all else fails, just send them wherever they were supposed to go.
return $redirect_to;
}
add_filter( 'login_redirect', 'developernoob_login_redirect', 10, 3 );
Steps to Follow:
- Swap out
home_url( '/subscriber-dashboard/' )
andhome_url( '/default-dashboard/' )
with your secret lairs (a.k.a. URLs). - This example is for role-based redirecting, but you can tweak it to your heart's content.
Method 2: Let a Plugin Do the Heavy Lifting 🏋️♀️
Don’t feel like coding? No problem! There’s a plugin for everything (well, almost), and this one’s called "Peter’s Login Redirect". It’s like hiring a butler for your redirects.
- First, get your plugin shopping on and install Peter’s Login Redirect from the WordPress plugin repository.
- After installing and activating, head over to Settings > Login/logout redirects and set up your redirects based on user roles, usernames, or even their astrological signs (okay, maybe not that last one... yet).
And voilà! You’ve now got a slick, custom login redirect system that’ll have your users feeling like they’re being personally escorted to their perfect page.
Happy redirecting! 🎉