Redirect user role to specific page on multisite, logging in from /wp-admin

I have a small issue. I am trying to make users with a specific role redirect to a specific page when logging in to a multisite. I can manage to do this if the user logs in directly to their site via example.com/thesite/wp-admin, but when logging in via example.com/wp-admin they are not redirected to the correct page, only to the sites dashboard.

This is how far I’ve got:

function primary_login_redirect( $redirect_to, $request_redirect_to, $user )
{
if ($user->ID != 0) {
$user_info = get_userdata($user->ID);
if ($user_info->primary_blog) {
$primary_url = get_blogaddress_by_id($user_info->primary_blog) . ‘wp-admin/edit.php?post_type=test’;
$site_id = $user_info->primary_blog;
if (($primary_url) && (current_user_can_for_blog($site_id, ‘user’))) {
wp_redirect($primary_url);
die();
}
}
}
return $redirect_to;
}
add_filter(‘login_redirect’,’primary_login_redirect’, 100, 3);

The problem is this part:

$site_id = $user_info->primary_blog;
if (($primary_url) && (current_user_can_for_blog($site_id, ‘user’))) {

Right now it redirects all roles to this page. But I cannot figure out why it isn’t working for only the user role user

Read more here:: Redirect user role to specific page on multisite, logging in from /wp-admin

Leave a Reply

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