plugins - Redirecting to home page after login as custom role WordPress

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 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;
    }

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;
    }
Share Improve this question edited Apr 4, 2019 at 20:30 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Apr 4, 2019 at 19:31 The DreamerThe Dreamer 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745627781a4636914.html

相关推荐

  • plugins - Redirecting to home page after login as custom role WordPress

    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.

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信