Assign a membership level to a user type on registration

Is there a way to assign a membership level to a user type on registration? I'm using a gravity form for user reg, and I'd like to assign all "subscribers" to a specific level and all "authors" to a different membership level.

I'm assuming it would be custom, I just need a little guidance in the direction to go.

  • Ash

    Hello jzvestor

    I hope you are well today and thanks for asking the question.

    Actually membership has it's own registration process separate from the default WordPress one. And I think to assign two levels to two user types, it needs some custom coding. You can post up a request on the Job Boards here though: https://premium.wpmudev.org/wpmu-jobs/

    Also I am tagging developer, if he can come with an easier solution.

    Cheers
    Ash

  • jzvestor

    I guess my main question is, where in the database is the membership level assign to an individual user? All the users I create appear in the membership users area (front end), they just need a level assigned. I know I could do it manually but it doesn't seem like it would be that hard to write something to do it behind the scenes, especially if ALL of a specific wp user type is assigned to 1 particular membership level.

    Sort of a If new user type = A, then membership level =B

  • Ash

    May be this is a solution for you. Please check and let me know if it works.

    Also note that, please keep a backup of previous files so that if it does not work, you can restore

    /wp-content/plugins/membership/membershipincludes/classes/membershipadmin.php:

    Line no: 2603, there is a line:
    $M_options['freeusersubscription'] = (isset($_POST['freeusersubscription'])) ? (int) $_POST['freeusersubscription'] : '';
    Put a line just after that
    $M_options['freeusersubscription2'] = (isset($_POST['freeusersubscription2'])) ? (int) $_POST['freeusersubscription2'] : '';

    Line no: 2832 to 2848, you will see a snippet:

    <tr valign="top">
    									<th scope="row"><?php _e('Use subscription','membership'); ?></th>
    									<td>
    										<select name='freeusersubscription' id='freeusersubscription'>
    											<option value="0"><?php _e('None','membership'); ?></option>
    										<?php
    											$subs = $this->get_subscriptions( array('sub_status' => 'active'));
    											if($subs) {
    												foreach($subs as $key => $sub) {
    													?>
    													<option value="<?php echo $sub->id; ?>" <?php if(isset($M_options['freeusersubscription']) && $M_options['freeusersubscription'] == $sub->id) echo "selected='selected'"; ?>><?php echo esc_html($sub->sub_name); ?></option>
    													<?php
    												}
    											}
    										?>
    										</select>
    									</td>
    								</tr>

    Replace with:

    <tr valign="top">
    									<th scope="row"><?php _e('Use subscription for Subscriber','membership'); ?></th>
    									<td>
    										<select name='freeusersubscription' id='freeusersubscription'>
    											<option value="0"><?php _e('None','membership'); ?></option>
    										<?php
    											$subs = $this->get_subscriptions( array('sub_status' => 'active'));
    											if($subs) {
    												foreach($subs as $key => $sub) {
    													?>
    													<option value="<?php echo $sub->id; ?>" <?php if(isset($M_options['freeusersubscription']) && $M_options['freeusersubscription'] == $sub->id) echo "selected='selected'"; ?>><?php echo esc_html($sub->sub_name); ?></option>
    													<?php
    												}
    											}
    										?>
    										</select>
    									</td>
    								</tr>
                                                                    <tr valign="top">
    									<th scope="row"><?php _e('Use subscription for Author','membership'); ?></th>
    									<td>
    										<select name='freeusersubscription2' id='freeusersubscription2'>
    											<option value="0"><?php _e('None','membership'); ?></option>
    										<?php
    											$subs = $this->get_subscriptions( array('sub_status' => 'active'));
    											if($subs) {
    												foreach($subs as $key => $sub) {
    													?>
    													<option value="<?php echo $sub->id; ?>" <?php if(isset($M_options['freeusersubscription2']) && $M_options['freeusersubscription2'] == $sub->id) echo "selected='selected'"; ?>><?php echo esc_html($sub->sub_name); ?></option>
    													<?php
    												}
    											}
    										?>
    										</select>
    									</td>
    								</tr>

    /wp-content/plugins/membership/membershipincludes/includes/functions.php:
    Line no: 322 to 333 you will see:

    function membership_assign_subscription($user_id) {
    
    	global $M_options;
    
    	if(!empty($M_options['freeusersubscription'])) {
    		$member = new M_Membership($user_id);
    		if($member) {
    			$member->create_subscription($M_options['freeusersubscription']);
    		}
    	}
    
    }

    Replace with:

    function membership_assign_subscription($user_id) {
    
    	global $M_options;
    
    	if(!empty($M_options['freeusersubscription'])) {
    		$member = new M_Membership($user_id);
    
    		if($member) {
    			if(get_user_role() == 'author')
    				$member->create_subscription($M_options['freeusersubscription2']);
    			else
    				$member->create_subscription($M_options['freeusersubscription']);
    		}
    	}
    
    }
    
    function get_user_role() {
    	global $current_user;
    
    	$user_roles = $current_user->roles;
    	$user_role = array_shift($user_roles);
    
    	return $user_role;
    }

    Please let me know if it works for you. Fortunately, if it works, then remember, this code will be lost when you will update your plugin. So, it's may worth to note down somewhere and apply again after updating.

    Also, if it doesn't work, I think the developer will be here soon with some tips.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.