Login redirect for Admins

Home Forums User Registration Login redirect for Admins

This topic contains 4 replies, has 2 voices, and was last updated by  Rodrigo Montiel 1 year, 2 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #3464

    Rodrigo Montiel
    Participant

    Hello,

    How can I make a redirect to the WP dashboard instead of the Account page only when the Admin logs in?

    I’ve tried using login_redirect in my functions.php file ( https://ibb.co/cUoVa9 ) but I’m also disabling WP login page like the example in the documentation. So now every user that logs in get redirected to the account page.

    Thank you in advance.

    #3472

    sanjeev3
    Member

    Hello,

    You can use the following code to your child theme’s functions.php

    function ur_admin_default_page(  $redirect, $user ) {
    
    	if( current_user_can( 'manage_options' ) ) {	
    		return admin_url();
    	} else {
    		return $redirect;
    	}
    }
    
    add_filter( 'user_registration_login_redirect', 'ur_admin_default_page', 10, 2 );

    Hope this helps!

    #3475

    Rodrigo Montiel
    Participant

    Hello, thank you for the answer.

    I’ve tried the code but it’s not taking any effect.
    When I enable WordPress default login page I get redirected to the dashboard for and Admin and to the Account page for subscribers, which is fine but I would like to keep the WP login hidden.

    If I do a login from the custom “Login” page or the “Account” page I get redirected to that same page after login, whether it is an admin or a subscriber.

    #3489

    sanjeev3
    Member

    Hello Montiel,

    Thank you for the clarification. You want to redirect to dashboard for the admins and users with other roles to the my account section while logging in from the user registration login form or my account form? Right? Here is the working code.

    function ur_admin_default_page(  $redirect, $user ) {
    
    	$roles = isset( $user->roles ) ? $user->roles : array();
    	if( in_array( 'administrator', $roles ) ) {
    		return admin_url();
    	}
    	else {
    		return $redirect;
    	}
    }
    
    add_filter( 'user_registration_login_redirect', 'ur_admin_default_page', 10, 2 );

    Hope this helps! Also, we really appreciate your review to our plugin, https://wordpress.org/support/plugin/user-registration/reviews/?filter=5

    Thank you for your support!
    WPEverest

    #3516

    Rodrigo Montiel
    Participant

    Hello sanjeev3,

    Yes! That was what I needed. Thank you!
    I just added the redirect to the account page for not admins, so now if a regular user logs in from the login page it also gets redirected to the Account page 🙂

    function ur_admin_default_page(  $redirect, $user ) {
    
    	$roles = isset( $user->roles ) ? $user->roles : array();
    	if( in_array( 'administrator', $roles ) ) {
    		return admin_url();
    	}
    	else {
            $redirect = get_permalink( ur_get_page_id( 'myaccount' ) );
    		return $redirect;
    	}
    }
    add_filter( 'user_registration_login_redirect', 'ur_admin_default_page', 10, 2 );

    Thank you

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.