Skip to:
Content
Pages
Categories
Search
Top
Bottom

Add users to groups based on email address


  • brettlewis
    Participant

    @brettlewis

    I’m making a website that has a few criteria and would appreciate some help thinking through some setup options.

    The site needs to have several user groups, I’m thinking buddypress to manage this that would allow users to post in their groups forum, their groups classifieds, etc…

    The groups should be automatically assigned to the user on registration based on their email address, think universities. (@school1.com, @school2.com) each would subscribe them to the group for School1 or School2; allowing them to access forums and classified specific to their university.

    Any ideas where to start on this? I’ve checked a few plugins but this issue is pretty specific and and moreso looking for where to start coding this.

Viewing 5 replies - 1 through 5 (of 5 total)

  • Henry Wright
    Moderator

    @henrywright

    These are the steps:

    1. Write a function which adds a user to a group
    2. Hook the function to an appropriate hook which fires on successful registration

    Notes:

    • groups_join_group() is the function you’ll need to use to add new members to a group. It will accept the new user’s ID.
    • Use the strpos() function to check if the university’s domain appears in the user’s email

    Hope this helps!


    VeeBeeGlobal
    Participant

    @veebeeglobal

    This is a great idea.
    There is a way to add fields to group creation (could add the email domain).
    As has been said, when someone registers you can check the domain against the group domains.

    Not sure of all the hooks/functions you need to use, but would love to hear if you are successful!!
    V


    64digital
    Participant

    @64digital

    Hi Bret,

    Did you find a way to do this as we need to do the same thing.

    Thanks

    Ben


    brettlewis
    Participant

    @brettlewis

    Try this 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(
    		      'school1.edu' => array(
    				'bp_id' => 2,
    			),
    		      'school2.edu' => array(
    				'bp_id' => 3,
    			)
    		      );
    
    	$output = substr(strrchr($email, "@"), 1);
    	groups_accept_invite( $user_id, $data[$output]['bp_id'] );
    
    }

    64digital
    Participant

    @64digital

    Thanks Bret, I will give that a go.

    Ben

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add users to groups based on email address’ is closed to new replies.
Skip to toolbar