Redirect Users to Specific Pages After Login in WordPress

How to redirect users after login in wordpress; Wordpress redirect user to specific page after login; Redirect after login wordpress plugin; Wordpress login redirect to previous page; Set login redirect url wordpress; Wordpress redirect users after registration; Custom login redirect wordpress; Wordpress redirect specific users after login; Wordpress login redirect based on user role; Redirect 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

<?php
// Redirect users to a specific page after login based on user role

function wp_dudecom_login_redirect($redirect_to, $request, $user) {
    // Check if the user is a valid WP_User object
    if (isset($user->roles) && is_array($user->roles)) {
        // Redirect based on user role
        if (in_array('administrator', $user->roles)) {
            return admin_url(); // Redirect administrators to the dashboard
        } elseif (in_array('editor', $user->roles)) {
            return home_url('/editor-dashboard'); // Redirect editors to a custom page
        } elseif (in_array('subscriber', $user->roles)) {
            return home_url('/subscriber-home'); // Redirect subscribers to a specific page
        } else {
            return home_url(); // Default redirect for other roles
        }
    } else {
        return $redirect_to; // Default redirect if user roles are not set
    }
}
add_filter('login_redirect', 'wp_dudecom_login_redirect', 10, 3);

// Redirect users to a specific page after registration
function wp_dudecom_registration_redirect() {
    return home_url('/welcome'); // Redirect to a welcome page after registration
}
add_filter('registration_redirect', 'wp_dudecom_registration_redirect');

// Redirect users to the previous page after login
function wp_dudecom_redirect_to_previous_page() {
    if (isset($_SERVER['HTTP_REFERER'])) {
        wp_safe_redirect($_SERVER['HTTP_REFERER']);
        exit;
    }
}
add_action('wp_login', 'wp_dudecom_redirect_to_previous_page', 10, 2);
?>

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.