Block Users from Sub-Site1 from Login in Sub-Site2

Hi there heroes.

Here's something we were not expecting: When a user register on Sub-Site 1 of our WPMU, he will also be able to login in any sub-site (sub-site2, 3, 4 and so on).

Is there any way we can block this? If a user register on sub-site 1, he want him to need to register again on sub-site 2 to login.

Thanks for your help on this!

  • Dimitris
    • Support Star

    Hey there Urbano,

    hope you're doing good and thanks for reaching us! :slight_smile:

    This shouldn't be happening by default, as a member should have access only to the subsite that registered for. Is this something that recently happened? Could you please try a theme/plugin conflict test as described here?
    https://premium.wpmudev.org/manuals/using-wpmu-dev/getting-support/
    This should narrow down the issue and we could further deal with it. If this is a live website, you may need to either enable a "maintenance mode" plugin for a while or create a staging environment, meaning a exact copy of your website in another location/folder in the same server that could be used for conflict and compatibility tests.

    Looking forward for your results,
    Dimitris

    • Urbano
      • The Incredible Code Injector

      Hi there Dimitris, thanks for your reply.

      We have created a new WPMU installation on the same server with no Plugins (only WPMUDEV dashboard) and it seems like users (subscribers and admins) are getting redirect to their respective sub-site by default.

      Could you possible confirm this please? Thank you.

      :beer:

    • Urbano
      • The Incredible Code Injector

      Hi there Dimitris, thanks again for your reply.

      It looks like the issue we experienced live it's not really an issue, it's the default behaviour of WPMU.

      This is a big issue to us since we need to obligate users registering on each site. Could you give me some hints on how to change this behaviour?

      For context, this is a platform where companies can create their own stores (based on WooCommerce). You can imagine the confusion when clients try to register on a store and they discover they are already registered on another non-related store also hosted at our platform, and they have to use the same login credentials.

      Could you please give me a hand figuring this out? Thank you! :beer:

  • Adam Czajczyk
    • Support Gorilla

    Hello Urbano!

    You're right about the default behavior. With Multisite every user that registers for any sites would have a "subscriber-level" access to other sites but - and this is funny but also confusing aspect - would not have an account on any of these other sites. So, such a user would be a kind of "virtual being" :slight_smile:

    This is the default behavior and I understand you would want to eliminate it so such users would be able to access only these sites they registered at, right? I think this code could be of help here:

    <?php
    add_action('template_redirect', function() {
    if (!is_user_member_of_blog()) {
    // here you could put some redirect e.g. to some "no access" or "please register" page or similar
    }
    });

    This code could be added as a MU plugin and I think it should be of help.

    Best regards,
    Adam

    • Urbano
      • The Incredible Code Injector

      Hi there Adam, nice to hear from you.

      Now we're talking! I've tried the code you provided, but I'm afraid it's not working. :slight_frown:

      Should I use some redirect in order to work properly? I'm not sure how to create the redirect. Could you possible give me some more directions?

      Thanks for your help! :beer:

  • Dimitris
    • Support Star

    Hey there Urbano,

    hope you're doing good and thanks for reaching us! :slight_smile:

    The code that my colleague Adam provided wasn't full as it contains the logic that should be used but not the actual redirect which should take the place of the comment line
    // here you could put some redirect e.g. to some "no access" or "please register" page or similar

    For that, you can use the wp_redirect() function and replace that line with something like

    $url = 'http://yourdomain.com/no-access';
    wp_redirect( $url );
    exit;

    where the $url variable should be a page already created informing your users.

    Hope that was some help, feel free to post back here if more assistance if required!
    Warm regards,
    Dimitris

    • Urbano
      • The Incredible Code Injector

      Hi there Dimitris, thank's for your follow-up!

      I'm trying to use the following code, but it's braking the website, I can't any access any site after adding it to the MU Plugin.

      /*add_action('template_redirect', function() {
      if (!is_user_member_of_blog()) {
      // here you could put some redirect e.g. to some "no access" or "please register" page or similar
      $url = '/minha-conta';
      wp_redirect( $url );
      exit;
      }
      });

      The URL should be the same but with an error message. Users Login in and Register on the same page ("My-Account"). Do you have any clues of what's causing this?

      Thanks again for all your help! :beer:

  • Predrag Dubajic
    • Support

    Hi Urbano,

    Can you try using this code and see if that works for you:

    <?php
    add_action('template_redirect', function() {
    	if (!is_user_member_of_blog() && !is_page( 'minha-conta' )) {
    		// here you could put some redirect e.g. to some "no access" or "please register" page or similar
    		$url = home_url().'/minha-conta/';
    		wp_redirect( $url );
    		exit;
    	}
    });

    Make sure that each subsite has "minha-conta" page which will be your registration page.

    Best regards,
    Predrag

    • Urbano
      • The Incredible Code Injector

      Hi there Predrag, thanks for your precious help.

      Unfortunately, once you visit site1.cloudstore.pt it always redirect to site1.cloudstore.pt/minha-conta. Should we apply this function on Login action only?

      Thanks again for your help on this.

  • Predrag Dubajic
    • Support

    Hi Urbano,

    I think I misunderstood your request here and though that you just wanted to redirect non-subsite users to custom page, after reading the thread again I see that the main issue here is having separate registrations across multisite subsites.

    Issue here is that this is not default WP multisite behaviour as all user registrations are kept in one place and users will be given subscriber account on subsite he registered but he will also be network member that keeps him logged in on other network sites.

    I'm afraid that there's no out of the box solution for this behaviour and I'm not familiar with the best solution for changing this but perhaps these guides will help you out with setting up your desired workflow:
    https://halfelf.org/2012/multisite-registration/
    https://premium.wpmudev.org/forums/topic/multisite-subsite-new-user-registration-that-does-not-go-through-main-site#post-551671
    http://wordpress.stackexchange.com/questions/124532/user-registration-on-subsite-in-multisite-configuration
    http://wordpress.stackexchange.com/questions/91755/user-registration-on-sub-site

    Best regards,
    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.