1

I have a user with custom role; but when I login as a user with custom role it redirects me to the homepage of my site. I tried using this code through my plugin but still it redirects to the homepage:

        $this->loader->add_action("login_redirect", $plugin_public,"user_login_handler", 99999, 3);// User Login Handler 

public function user_login_handler($redirect_to, $request, $user)
    {
        //$user = new WP_User($user);
        //is there a user to check?
        // require_once plugin_dir_path(dirname(__FILE__)) . 'admin/models/tscgd-user.php';
        //check for Dashboard User
        if ( ! is_wp_error( $user ) ) {
        if (array_intersect($this->user_role_slug, (array) $user->roles)) {

                // redirect them to another URL, in this case, the homepage
                $redirect_to = get_site_url() . "/wp-admin/";


        }
    }
        return $redirect_to;
    }
1

This is what I'm using, it redirects "shop_managers" to the admin orders page.

function my_login_redirect( $redirect_to, $request, $user ){
    if (isset($user->roles) && is_array($user->roles)) {
        if (in_array('shop_manager', $user->roles)) {
            $redirect_to = '/wp-admin/edit.php?post_type=shop_order';
        }
    }

    return $redirect_to;
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

Obviously, you will need to change 'shop_managers' and '/wp-admin/edit.php?post_type=shop_order' to what you need.

Tested and working.

  • i want to redirect it to /wp-admin/index.php for the users with a specific role – The Dreamer Apr 4 at 19:59
  • What is the name of your role? – RiddleMeThis Apr 4 at 20:00
  • yes i read it. the above code is working in my other sites.role name is ANMO Subscriber – The Dreamer Apr 4 at 20:00
  • I'm not familar with your code, I've never seen $this->loader->add_action, I just gave you what works for me. All you need to do is add in the correct user role and redirect path. – RiddleMeThis Apr 4 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.