Assign Users to Single Multisite Blog on Registration.

This question is a bit a followup to one I had last week, In this instance I am looking to create a multi site blog that assigns users to a specific blog based on their email domain. In this example I would want the site to function as such:

User: name@domain1.com -> Site: domain1.mydomain.com
User: name@domain2.com -> Site: domain2.mydomain.com

So, Users at Domain1 would be added as users to ONLY the network site Domain1.

The sites would already be generated, and only specific domains would be allowed to register.

Thank you in advance for any guidance.

  • Vinod Dalvi

    Hi @brettlewis,

    I hope you are well today and thank you for your question.

    So you have enabled user registration on your multisite but the sub sites creation are not allowed as they are already created and you want to add the newly registered user to the respective subsites based on the email domain.

    Is this right?

    What if the registered user email domain doesn't match with any subsites domain?

    Should we delete such users or should restrict such users from registering on your site by validating their email id?

    Are you using BuddyPress or any other plugin that changes the default user registration form?

    If you can please share your site URL so that i can see the registration page.

    Please advise.

    Kind Regards,
    Vinod Dalvi

  • brettlewis

    Vinod,

    Yes, you are right. Ideally the user flow would be something like this:

    User signs up on home page of site 'domain.com' with the email address user@domain1.com, they get automatically subscribed to JUST domain1.domain.com on the network.

    Right now I just have a whitelist of domains, which is built in to wordpress network settings as "limited email registrations"

    yes, I will be using buddypress as well as bbpress and potentially membership 2 plugin.

    This site is in initial development phase and is just local, so I don't have a url for it.

    Thank you!

  • Ash

    Hello @brettlewis

    Please try the following code:

    add_action( 'user_register', 'assign_membership_on_register', 10, 1 );
    function assign_membership_on_register( $user_id ){
    	$user_info = get_userdata( $user_id );
    	$email = $user_info->user_email;
            $domain = getBetween( $email, '@', '.' );
    
            $blog_id = get_id_from_blogname( $domain );
            add_user_to_blog( $blog_id, $user_id, 'subscriber' );
    }
    
    if( ! function_exists( 'getBetween' ) ){
        function getBetween($content,$start,$end){
            $r = explode($start, $content);
            if (isset($r[1])){
                $r = explode($end, $r[1]);
                return $r[0];
            }
            return '';
        }
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

    • brettlewis

      In my case the email domain won't always correspond with the exact name of the network blog. I want to try to assign it by ID, if possible.

      Here is some code I was playing with to do that, as well as assign membership 2 rights to the user on their blog as well.

      Any guidance on whats wrong here?

      add_action( 'user_register', 'assign_membership_on_register', 10, 1 );
      function assign_membership_on_register( $user_id ){
          $user_info = get_userdata( $user_id );
          $email = $user_info->user_email;
          $data = array(
                    'domain1' => array(
                      'network_id' => 1,
                      'ms_id' => 1,
                  ),
                    'domain2' => array(
                      'network_id' => 2,
                      'ms_id' => 1,
                  )
                    );
              $output = getBetween( $email, '@', '.' );
      
              $blog_id = get_id_from_blogname( $data[$output]['network_id'] );
              add_user_to_blog( $blog_id, $user_id, 'subscriber' );
      
              // subscribe to memebership 2
              $member = MS_Factory::load( 'MS_Model_Member', $user_id );
              $subscription = $member->add_membership( $data[$output]['ms_id'] );
      
              if ( $member->has_membership() ) {
                  $member->is_member = true;
              } else {
                  $member->is_member = false;
              }
              $member->save();
      }
      
      if( ! function_exists( 'getBetween' ) ){
          function getBetween($content,$start,$end){
              $r = explode($start, $content);
              if (isset($r[1])){
                  $r = explode($end, $r[1]);
                  return $r[0];
              }
              return '';
          }
      }
  • brettlewis

    oops

    I see what's wrong with that last reply. Here is fixed solution.
    I activated network mode on memebership2 to apply membership rules across the board, so I took the membership function out.

    add_action( 'user_register', 'assign_membership_on_register', 10, 1 );
    function assign_membership_on_register( $user_id ){
        $user_info = get_userdata( $user_id );
        $email = $user_info->user_email;
        $data = array(
                  'domain1' => array(
                    'network_id' => 2,
                ),
                  'domain2' => array(
                    'network_id' => 3,
                )
                  );
            $output = getBetween( $email, '@', '.' );
            add_user_to_blog( $data[$output]['network_id'], $user_id, 'subscriber' );
    }
    
    if( ! function_exists( 'getBetween' ) ){
        function getBetween($content,$start,$end){
            $r = explode($start, $content);
            if (isset($r[1])){
                $r = explode($end, $r[1]);
                return $r[0];
            }
            return '';
        }
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.