programmatically create users at various level

I have written an api for my WP install to automatically add users when they are created by another separate application. This application can create different access levels.

What I'd like to do is map each created user to an access level I've created with the Membership plugin. What functions should I look at in the plugin?

Or if I simply create different roles when I create the user and link those roles to access level in the Membership plugin will that work?

  • Jack Kitterhing

    Hi there @thesrpr

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

    I have taken a look and think I may have found some useful functions/snippets of code :slight_smile:

    There's this

    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']);
    		}
    	}
    
    }

    The most notable part being function membership_assign_subscription($user_id) {

    That can be found within /wp-content/plugins/membership/membershipincludes/includes/functions.php:
    Line no: 322 to 333.

    That can be used to assign a subscription.

    My colleague then done this for use with a specific user role

    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;
    }

    Then there's also

    membership_subscription_form_before_subscriptions
    membership_subscription_form_before_paid_subscriptions
    membership_subscription_form_after_paid_subscriptions
    membership_subscription_form_after_subscriptions
    
    current_user_is_member()
    current_user_on_level($level_id)
    current_user_on_subscription($sub_id)

    Some of which can be found here /membership/membershipincludes/includes/membership-includes/subscription.form.php

    Thank you!

    Kind Regards
    Jack.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.