What is the better way to restrict a user of any role being logging into a blog of wordpress multisite network if he/she is not assigned to that particular blog as a user even though he is an administrator of another blog in that network. I hope it can be done using get_blogs_of_user( $user ); function where $user is the username of person who trying to login.

EDIT: My code as follows.

 $info = array();
    $info['user_login'] = $_POST['username'];
    $info['user_password'] = $_POST['password'];
    $info['remember'] = true;

 $user_signon = wp_signon($info, false);
    if (is_wp_error($user_signon)) {
        echo json_encode(array('loggedin' => false, 'message' => __('Invalid Credentials.')));
    } else {
    $get_blogs = get_blogs_of_user(get_user_by('login',$info['user_login'])->ID);
    $is_has_access = array_search(get_current_blog_id(), array_column($get_blogs, 'userblog_id'));
    if (!$is_has_access) {
        echo json_encode(array('loggedin' => false, 'message' => __('You have no permission to access.')));
    } else {
            echo json_encode(array('loggedin' => true, 'message' => __('Login successful')));

Is there any better ways?

  • 1
    A user can be an administrator on Site A and yet have no access to Site B (ie, she'd have the same access to Site B as any non-logged-in user visiting Site B). This is already part of Multisite.
    – Pat J
    Commented May 15, 2016 at 22:07
  • That is wanted. But in my install of multisite, if user is admin of site A, then he can login to site B. But my aim is to restrict his login to site B
    – mpsbhat
    Commented May 16, 2016 at 3:34
  • Well, he can log in by going to site B, but he won't be able to do anything there. IIRC, he'll be given a list of links to sites where he can do things. You're trying to deny him the ability to log in at site B?
    – Pat J
    Commented May 16, 2016 at 12:44
  • Yes... I don't want the user to login to site B
    – mpsbhat
    Commented May 17, 2016 at 3:48

1 Answer 1


is_user_logged_in() is a pluggable function, which means you can override its functionality. That's probably where I'd start.

Something like this may do what you're looking for. This is untested code and comes with no warranties of any kind.

function is_user_logged_in() {
    $user = wp_get_current_user();
    $current_blog_id = get_current_blog_id();
    $allowed = false;
    $blogs_of_user = get_blogs_of_user( $user->ID );
    foreach( $blogs_of_user as $blog ) {
        if( $blog->userblog_id == $current_blog_id ) {
            $allowed = true;

    if( ! $allowed ) {
        return false;

    return $user->exists();



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.