Home>
I want to redirect with WordPress user_role_editor.

I'd like to create a new permission and change the page after login, but it doesn't work.
I added the following code to function.php, but it goes to the officer with all privileges.

This is a new permission created by 'Customer'.

// Redirect by privilege at login
 function redirect_login_page () {
  if (current_user_can ('Customer')) {
    $url = home_url ('/ customer', 'http');
    wp_safe_redirect ($url);
    exit ();
  } else {
    $url = home_url ('/ officer', 'http');
    wp_safe_redirect ($url);
    exit ();
  }
}
add_action ('wp_login', 'redirect_login_page');


I considered the influence of other plug-ins, but I think that the function is alive as long as I go to the officer.

  • Answer # 1

    Are you sure that the permissions are valid (whethercurrent_user_canpasses in index.php)?
    I think it's code that seems to work if it's OK, butwp_loginaccepts aWP_Userobject as an argument, so try it out.

    add_action ('wp_login', 'redirect_roll', 10, 2);// ← Changed
    function redirect_roll ($user_login, $user) {// ← Changed
        if ($user->has_cap ('Customer')) {
            wp_safe_redirect (home_url ('/ customer', 'http'));
            exit ();
        } else {
            wp_safe_redirect (home_url ('/ officer', 'http'));
            exit ();
        }
    } // not tested

    【user.php in tags/5.3/src/wp-includes – WordPress Trac】
    https://core.trac.wordpress.org/browser/tags/5.3/src/wp-includes/user.php#L110

    do_action ('wp_login', $user->user_login, $user);

  • Answer # 2

    Sorry. Eventually I created a new permission and solved it with another plug-in.