WordPress.org

Ready to get started?Download WordPress

Forums

Restrict Users to be Able to Log in to Only Their Associated Sites (3 posts)

  1. DeltaR
    Member
    Posted 1 year ago #

    I've been searching for a solution to this issue but have been unable to find one.

    I'm setting up a Multisite installation where I want all client sites to be public, but users that are given logins should only be able to log in to sites that they are associated with.

    For instance, when a user logs in to their site and then navigates to another site in the network, it should not show the navigation bar at the top of the page, nor should it say "Logged in as [username]" in the comments section and allow them to post comments as that user.

    There is a case that users might have logins for multiple sites in the network. In this case the single login should be shared among just the sites they are associated with.

    Essentially each site is separate, but can have shared users in certain cases.

    Are there any plugins that provide this or similar functionality? The "More Privacy Options" and "Network Privacy" plugins don't do what I need.

    Thank you.

  2. Since all logins route through the same place, they'll always be able to log in on all sites.

    That said, you could write a check for if a user is a member of that site and, if not, redirect them to the front end any time they try to go to the back end.

    I think this will do it. Put it in an mu-plugin file.

    add_filter( 'login_redirect', 'DeltaR_login_redirect', 10, 3 );
    function DeltaR_login_redirect( $redirect_to, $request, $user ) {
        // Is there a user?
        if ( is_array( $user->roles ) && is_user_member_of_blog() ) {
                 return home_url( '/wp-admin/' );
            else
                return home_url();
        }
    }

    As for comments, that's a bit harder, and you'd have to run that same check in the theme, or for every comment block...

  3. DeltaR
    Member
    Posted 1 year ago #

    Hm, I was hoping there was a plugin out there that at least got close to what I want to do, heh.

    I might have to go the custom route. Thanks for the code snippet!

    I think I might have to use that code, hide the admin bar on the front end, and possibly hook into comment_form_before to do something with the comments section.

Topic Closed

This topic has been closed to new replies.

About this Topic