In my installation I have 3 sites. I would like to have one user on each page and can login only on this site.

However, default multisite behavior is different. The user assigned to site1 can also log in to site2 and site3, but there is no assigned role.

Summarizing. I would like to achieve such a situation that user1 can log in only on site1, user2 on site2, etc.

Please help me.

1 Answer 1


Put this in a .php file, upload it as a plugin, and network activate it. Read the comments in the code for more information.

Let me know if you have questions or if it does not work well for you. I made it so Super Administrators can use all sites no matter what.


Plugin Name: No Role No Login
Description: If the user in a multisite has no role for a site, he will not be able to access it while logged in. Does not affect super administrators.
Author: Nikolay Nikolov
Version: 1.0.0

add_action( 'init', 'no_role_no_login' );

function no_role_no_login() {

    // If the user is logged-in and not a super administrator we continue
    if ( is_user_logged_in() && ! current_user_can( 'manage_network' ) ) {

        // If this is a logout attempt, we do nothing, so we allow it
        if ( ( ( isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] )
            || ( isset( $_SERVER['PHP_SELF'] ) && '/wp-login.php' === $_SERVER['PHP_SELF'] ) )
            && isset( $_GET['action'] ) && 'logout' === $_GET['action'] ) {

        // If this is not a logout attempt, we will check if the user has a role for the site and deny access if he does not (and show a message)
        } else {

            $get_users = get_users(
                    'blog_id' => get_current_blog_id(),
                    'include' => array( get_current_user_id() )

            if ( empty( $get_users ) ) {
                wp_die( 'You cannot access this site while logged in. To view it, you have to <a href="' . wp_logout_url() . '">logout</a>.' );


  • I had easier solution, but it's not as good as I want. if ( is_user_logged_in() && ( !current_user_can( 'administrator' ) || !current_user_can( 'contributor' ) || !current_user_can( 'student' ))) { wp_logout(); } But when I'm logged in on site1, and go to site2 I'm automaticly logged out on every site. Better if I stay logged on site1 and logged out on other sites. Mar 24, 2018 at 15:41
  • And of course i'm very glad for your answer! Mar 24, 2018 at 15:47
  • @MainpagePL Well, yeah, this is exactly why I didn't make it that way :). You cannot logout the user on one site. The multisite has global users and they are logged out everywhere.
    – Nikolay
    Mar 24, 2018 at 16:55

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.