How to Implement Role-Based Redirects After Login in WordPress Without Plugins

How to Implement Role-Based Redirects After Login in WordPress Without Plugins featured illustration

By

On

You can implement role-based redirects after login in WordPress without plugins by adding a custom PHP function to your child theme’s functions.php file. This function hooks into the WordPress login system, checks the user’s assigned role using native functions like get_userdata(), and directs them to a specific URL using wp_redirect().

What are WordPress User Roles and Why Redirect Them?

WordPress user roles are pre-defined permission sets that determine what a specific user can and cannot do on your website. Standard roles include Administrator, Editor, Author, Contributor, and Subscriber. Sometimes, you may also have custom roles created by membership or e-commerce plugins like WooCommerce, such as “Customer” or “Student”.

What are WordPress User Roles and Why Redirect Them?. Three connected stages illustrating the section's sequence for How to Implement Role-Based Redirects After Login in WordPress Wi.
What are WordPress User Roles and Why Redirect Them?

By default, WordPress sends almost every user to the backend dashboard (or their profile page) after they log in. However, if you run an online school for students in Dhaka or a multi-author blog, sending a regular reader or student to the WordPress admin panel can be confusing and unnecessary. Role-based redirects allow you to send a student straight to their learning dashboard, an author to their draft list, and an administrator to the main backend.

Where Should You Put the Custom Code Safely?

Before adding any code to your WordPress site, you must use a child theme or a site-specific code snippet plugin. Never paste custom code directly into your main parent theme’s functions.php file, because when the theme updates, all your changes will be erased and your site might break.

Think of your WordPress theme like a rented house: if you paint the walls directly (parent theme), the landlord will paint over it when you renew. But if you put up a removable partition (child theme), your custom work stays safe no matter what happens to the main house.

  1. Log in to your WordPress hosting control panel or use an FTP client to access your website files.
  2. Navigate to wp-content/themes/ and locate your active child theme folder. If you do not have a child theme, you can create a basic one or use a safe code snippet manager plugin.
  3. Open the functions.php file inside your child theme folder using a text editor.

How Do You Write the PHP Code for Role-Based Redirects?

You can implement the redirection by hooking into the login_redirect filter provided by WordPress. This filter intercepts the default login destination URL and lets you change it dynamically based on who is logging in.

How Do You Write the PHP Code for Role-Based Redirects?. Vertical checklist illustration with three marked items for How to Implement Role-Based Redirects After Login in WordPress Wi.
How Do You Write the PHP Code for Role-Based Redirects?

Here is a clean, lightweight PHP code snippet that checks user roles and sends them to different URLs:

function anontow_role_based_redirect( $redirect_to, $request, $user ) {
    // Check if the user is actually logged in and has roles
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        
        // If the user is an Administrator, send them to the dashboard
        if ( in_array( 'administrator', $user->roles ) ) {
            return admin_url();
        }
        
        // If the user is a Subscriber, send them to a custom welcome page
        if ( in_array( 'subscriber', $user->roles ) ) {
            return home_url( '/student-dashboard/' );
        }
        
        // For all other roles, send them to the homepage
        return home_url();
    }
    
    return $redirect_to;
}
add_filter( 'login_redirect', 'anontow_role_based_redirect', 10, 3 );

In this code, login_redirect is the hook that tells WordPress to run our custom function named anontow_role_based_redirect right after a successful login. We check the array of roles assigned to the user object. If they are a subscriber, they go to your site’s /student-dashboard/ page. Otherwise, they fallback to standard behavior or the homepage.

How Can You Test and Troubleshoot Your Redirects?

Testing your custom code properly prevents you from getting locked out of your own website. Always perform your checks in a controlled environment or keep an open administrator session in a separate browser tab.

How Can You Test and Troubleshoot Your Redirects?. Three-step horizontal workflow diagram for How to Implement Role-Based Redirects After Login in WordPress Wi.
How Can You Test and Troubleshoot Your Redirects?
  • Keep an active admin tab: While editing code, stay logged into WordPress in an incognito window or secondary browser so you can quickly fix errors if something goes wrong.
  • Clear your cache: If you use caching plugins or server-side caching (like Cloudflare), clear your cache after saving functions.php.
  • Test with dummy accounts: Create a test user account for each role (such as a Subscriber or Editor) and log in through the standard login page to verify they land on the correct URL.

If you make a syntax error in your PHP code, your site might show a “White Screen of Death” or a 500 Internal Server Error. If this happens, access your site files via FTP or your host’s File Manager, remove the code you just added, and double-check your brackets and semicolons.

Leave a Reply

Olivia Masskey

Carter

is a writer covering health, tech, lifestyle, and economic trends. She loves crafting engaging stories that inform and inspire readers.