php – WordPress Multisite login_redirect to primary blog and specific page based on role

Question

I’m stumped trying to redirect my users to a specific page on their primary blog when they login to the root/main site on a MU setup. I have the site editors with a specific ‘editor’ role and then tenants with ‘subscriber’ role. The following code (in a plugin in MU folder) redirects the users with one of the 3 editor roles but will not redirect those with ‘subscriber’ role. Any help would be greatly appreciated! Thanks!

public function login_redirect($redirect_to, $requested_redirect_to, $user) {
    $user = wp_get_current_user();
    
    if ( (in_array( 'editor-tier3', $user->roles )|| in_array( 'editor-tier2', $user->roles ) || in_array( 'editor-tier1', $user->roles )))  {
        
        if ($user->ID != 0 && $user->ID != 1) {
    
            $user_info = get_userdata($user->ID);
    
            if ($user_info->primary_blog) {
    
                $primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'management-dashboard/';
    
                if ($primary_url) {
    
                    wp_redirect($primary_url);
    
                    die();
    
                }
    
            }
    
        }
    }
    
    elseif (in_array('subscriber', $user->roles))  {
    
        if ($user->ID != 0 && $user->ID != 1) {
    
            $user_info = get_userdata($user->ID);
    
            if ($user_info->primary_blog) {
    
                $primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'tenant-dashboard/';
    
                if ($primary_url) {
    
                    wp_redirect($primary_url);
    
                    die();
    
                }
    
            }
    
        }   
    
        
    }
        
       // return $redirect_to;
    
    }

0
user3311868 2 years 2022-05-11T10:40:48-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse