Snippet

Redirect Users to Specific Pages After Login in WordPress

How to redirect users after login in wordpressWordpress redirect user to specific page after loginRedirect after login wordpress pluginWordpress login redirect to previous pageSet login redirect url wordpressWordpress redirect users after registrationCustom login redirect wordpressWordpress redirect specific users after loginWordpress login redirect based on user roleRedirect wordpress users to homepage after login

Explanation

Want to send users to a specific page after they log in? This code does just that, based on their role in WordPress.

  • Admins: They get whisked away to the dashboard.
  • Editors: They land on a custom page just for them.
  • Subscribers: They head to a special subscriber page.
  • Everyone else: They go to the homepage.

If a user signs up, they’ll be redirected to a welcome page. This is a nice touch to greet new members.

And if someone logs in from a specific page, they’ll be sent right back to where they came from. This is handy if you want to keep the flow smooth and uninterrupted.

These redirections are set up using WordPress filters and actions, which are like little hooks that let you change how things work without touching the core files. Just pop this code into your theme’s functions.php file, and you’re good to go!

Code

1<?php 2// Redirect users to a specific page after login based on user role 3 4function wp_dudecom_login_redirect($redirect_to, $request, $user) { 5 // Check if the user is a valid WP_User object 6 if (isset($user->roles) && is_array($user->roles)) { 7 // Redirect based on user role 8 if (in_array('administrator', $user->roles)) { 9 return admin_url(); // Redirect administrators to the dashboard 10 } elseif (in_array('editor', $user->roles)) { 11 return home_url('/editor-dashboard'); // Redirect editors to a custom page 12 } elseif (in_array('subscriber', $user->roles)) { 13 return home_url('/subscriber-home'); // Redirect subscribers to a specific page 14 } else { 15 return home_url(); // Default redirect for other roles 16 } 17 } else { 18 return $redirect_to; // Default redirect if user roles are not set 19 } 20} 21add_filter('login_redirect', 'wp_dudecom_login_redirect', 10, 3); 22 23// Redirect users to a specific page after registration 24function wp_dudecom_registration_redirect() { 25 return home_url('/welcome'); // Redirect to a welcome page after registration 26} 27add_filter('registration_redirect', 'wp_dudecom_registration_redirect'); 28 29// Redirect users to the previous page after login 30function wp_dudecom_redirect_to_previous_page() { 31 if (isset($_SERVER['HTTP_REFERER'])) { 32 wp_safe_redirect($_SERVER['HTTP_REFERER']); 33 exit; 34 } 35} 36add_action('wp_login', 'wp_dudecom_redirect_to_previous_page', 10, 2); 37?>

Instructions

To implement the user redirection after login based on their role, follow these steps:

File Location: functions.php (located in your active theme's directory) or a custom plugin file.

Prerequisites:

  • Access to your WordPress site's file system (via FTP or a file manager).
  • Basic understanding of how to edit PHP files.

Implementation Steps:

  1. Backup your site: Before making any changes, ensure you have a backup of your site.
  2. Access the file: Navigate to your theme's directory and open the functions.php file. Alternatively, if you prefer using a plugin, open your custom plugin file.
  3. Add the code: Copy the provided code snippet and paste it at the end of the functions.php file or your plugin file.
  4. Save the changes: After pasting the code, save the file.
  5. Test the functionality: Log in with different user roles to ensure they are redirected to the correct pages as specified in the code.

If you encounter any issues or need further assistance, consider reaching out to wp-dude.com for expert help with implementation or more advanced functionality.