• I have a WordPress multisite installation. When the user (who is not a super admin) logs in and then opens the dashboard of the main page (example.com/wp-admin), he gets the message below:

    You attempted to access the “Example.com” dashboard, but you do not currently have privileges on this site. If you believe you should be able to access the “Example.com” dashboard, please contact your network administrator.

    If you reached this screen by accident and meant to visit one of your own sites, here are some shortcuts to help you find your way.

    Your Sites mysubsite Visit Dashboard | View Site

    I would like to automatically redirect these users to Visit Dashboard link instead of displaying the above message.

    I found these below PHP codes:

    This redirects the user to the primary dashboard when they try to log in to the dashboard of another subsite.

    add_filter('login_redirect', function($redirect_to, $request_redirect_to, $user)
    {
        global $blog_id;
        if (!is_wp_error($user) && $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/';
                $user_blogs = get_blogs_of_user($user->ID);
    
                //Loop and see if user has access
                $allowed = false;
                foreach($user_blogs as $user_blog)
                {
                    if($user_blog->userblog_id == $blog_id)
                    {
                        $allowed = true;
                        break;
                    }
                }
    
                //Let users login to others blog IF we can get their primary blog URL and they are not allowed on this blog
                if ($primary_url && !$allowed)
                {
                    wp_redirect($primary_url);
                    die();
                }
            }
        }
        return $redirect_to;
    }, 100, 3);

    And this one redirects all logged in users:

    function check_if_user_needs_redirecting(){
        $active_blog = get_active_blog_for_user(get_current_user_id());
        $blogid = get_current_blog_id();
        if ($blogid != $active_blog->blog_id){
            header('Location: '.$active_blog->siteurl.'/wp-admin/');
            exit;
        }
    }
    add_action( 'init', 'check_if_user_needs_redirecting' );
    • This topic was modified 4 years, 5 months ago by Feriman.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redirect logged in users to their primary dashboard’ is closed to new replies.