Redirect user role to a specific page on login in a multisite network?

I have been working on this for some time now. I want to redirect a specific user role to a specific page in the backend on a Multisite install.
I have been able to do this finally, but I get an error that the page was redirected incorrectly, but I can see that the URL is correct.

This is the code:

add_action(‘wp_login’, function(){
global $logging_in;
$logging_in = true;
});

add_action(‘current_screen’, ‘primary_login_redirect’);
function primary_login_redirect( $user ) {
global $logging_in;
$userID = get_current_user_id();
$user_info = get_userdata($userID);
if (($logging_in = true) && ($user_info->primary_blog)) {
$primary_url = get_blogaddress_by_id($user_info->primary_blog) . ‘wp-admin/edit.php?post_type=visitor’;
$site_id = $user_info->primary_blog;
if (!current_user_can_for_blog($site_id, ‘create_users’)) {
wp_redirect($primary_url);
die();
}
}
}

To be able to check for capabilities I had to create the wp_login function which sets $logging_in. If I login with a user which has the create_users capability I go straight to Dashboard, but if I login with a user who doesn’t have this capability I will be redirected to example.com/subsite/wp-admin/edit.php?post_type=visitor (where subsite is the users main multisite). But for some reason I get an error when this redirect happens.
The page isn’t redirecting properly shown in the browser.

Read more here:: Redirect user role to a specific page on login in a multisite network?

Leave a Reply

Your email address will not be published. Required fields are marked *