Skip to content
wpbeaches
  • Home
  • About
  • Services
  • Work
    • Template Library
    • Web Development
    • Portfolio
  • Contact
  • Blog
    • ACF
    • Beaver
      • Beaver Builder
      • Beaver Theme
      • Beaver Themer
    • Genesis
    • jQuery
    • macOS
    • WordPress
    • WooCommerce

Redirect an admin user role on login with login redirect

January 9, 2021 - 2 Comments

You can redirect a user based on their WordPress role with the login_redirect filter.

add_filter( 'login_redirect', 'themeprefix_login_redirect', 10, 3 );
/**
 * Redirect user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user's data.
 * @return string
 */
function themeprefix_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}

In the above any admin users to go wp-admin on login every other role goes to the site home URL.

 

add_filter( 'login_redirect', 'themeprefix_login_redirect', 10, 3 );
/**
 * Redirect user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user's data.
 * @return string
 */
function themeprefix_login_redirect( $redirect_to, $request, $user ){

    //is there a user to check?

    if ( isset( $user->roles ) && is_array( $user->roles ) ) {

        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {

            $redirect_to = '/wp-admin/'; // Your redirect URL
        }
    }

    return $redirect_to;
}

In the amended code above, any admin is forced to wp-admin as the $redirect_to variable is reset.

 

In an update to this post – here is an alternative option to redirect based on a certain role using the action wp_login.

add_action( 'wp_login', 'prefix_login_redirect_based_on_roles', 10, 2 );
/**
 * Redirect Shop Manager to WC Orders
 * @link https://stackoverflow.com/questions/30124931/how-to-redirect-a-user-with-specific-role-to-a-specific-page-after-login-in-wo
 */
function prefix_login_redirect_based_on_roles( $user_login, $user ) {

    if( in_array( 'shop_manager',$user->roles ) ){
        exit( wp_redirect('/wp-admin/edit.php?post_type=shop_order' ) );
    }   
}

So in the code above the role of ‘shop_manager’ gets redirected to the WooCommerce Orders screen, change it to meet our needs.

Related Posts:

  • Hide WordPress Admin Toolbar Based On User Role
    Hide WordPress Admin Toolbar Based On User Role
  • Reset Forgotten Admin Password on macOS Monterey and macOS Big Sur
    Reset Forgotten Admin Password on macOS Monterey and macOS…
  • Redirect a Custom Post Type's Single and Archive Pages in WordPress
    Redirect a Custom Post Type's Single and Archive Pages in…
  • Enable the root user in macOS Monterey and earlier macOS versions
    Enable the root user in macOS Monterey and earlier macOS…
  • Adding a Site Login/Logout Link to an existing Menu in WordPress
    Adding a Site Login/Logout Link to an existing Menu in…
  • Block xmlrpc.php and wp-login.php via NGINX
    Block xmlrpc.php and wp-login.php via NGINX
  • Block wp-login.php and xmlrpc.php via fail2ban on RunCloud
    Block wp-login.php and xmlrpc.php via fail2ban on RunCloud
Categorized WordPress Tagged login_redirect
Get Beaver Builder Now!

Tags

ACF apache archive beaver beaver builder beaver theme beaver themer bootstrap cache category checkout cpt css customizer filter flexbox footer form genesis header homebrew htaccess iconfonts image javascript jquery loop markup menu meta mysql php pre_get_posts repeater search filter pro serverpilot shortcode slider taxonomy template UI/UX while widget woocommerce wp-cli

Donate

If you found something that helped you out and would like to make a donation, that's great

PowerPack Beaver Builder Addon

Our Location

We are based in the Northern Beaches of Sydney and work with both local and overseas based clients.

Work With Us

Let us know your web requirements, see our services and use the contact form to get in touch and start the ball rolling.

Check Us Out

  • Home
  • Blog
  • About
  • Services

© 2017 · NEIL GOWRAN · POWERED BY WORDPRESS, BEAVER, AND HOSTED ON RUNCLOUD AND RACKNERD

Scroll To Top