[ Membership 2 Pro ]Creating/Adding/Restricting/upgrading Members to Memberships Remotely with API

I need to add my members to the membership remotely. Basically i want to use the API to Create/Add/Restrict/Upgrade my members.

I am using Rapid Action Profits and have the whole affilaite system setup and ready to go. How it is going to work for me is

1) My new customers will land on the Front end download page or the one time offer upgrade depending on how much they bought. Ill have a form here which they fill in that creates there membership for them. There will be 2 different memberships the customers who buy the oto will recieve more access. I have Membership pro 2 memberships setup already.
2) If the memebr decides to upgrade at a future time i will want to upgrade there account remotely also.
3) with the script i am using i can also call a URL when the user cancels there account with paypal. So what i want to call is a page that restricts access again. Maybe ill create another account that has basic access and the change there status to this.

Can anyone help me out with setting up a script that connects to my Membership 2 Pro plugin and give me access to do the above??

Regards,
Kieron

  • Predrag Dubajic

    Hey Kieron,

    Hope you're doing well today :slight_smile:

    This request is bit more complicated and goes beyond support we offer here as we don't have enough manpower to assist on such modifications.

    If you already have some code ready for this I could call in some of our second level support dev guys to have a look and point you in right direction, but if you are starting from scratch you can post a job in our job board where you can hire a developer to assist further:
    https://premium.wpmudev.org/wpmu-jobs/
    Please note that, no WPMU official staff members are allowed to work in the job board.

    Best regards,
    Predrag

  • Predrag Dubajic

    Hi Kieron,

    API documentation is located under Membership 2 > Help tab in your admin section and it should lead you to URL like this:
    http://yourdomain.com/wp-content/plugins/membership/docs/namespaces/default.html

    Also could you point out where the data for Membership 2 Pro is stored in my database. I just cant find it...lol

    I believe it's under dbname_usermeta, all that starts with ms_ :slight_smile:

    Let us know when we can help you out :slight_smile:

    Best regards,
    Predrag

  • kgarvey

    Hi Predrag,

    I can no create a user remotely but was wondering if you could point me in the direction of assigning this user to a membership in Membership 2 Pro.

    here is the code i have so far

    <?php
    // ADD NEW USER TO WORDPRESS
    // ----------------------------------
    // Put this file in your Wordpress root directory and run it from your browser.
    // Delete it when you're done.
    
    require_once('wp-blog-header.php');
    require_once('wp-includes/registration.php');
    
    // ----------------------------------------------------
    // CONFIG VARIABLES
    // Make sure that you set these before running the file.
    $newusername = 'user321';
    $newpassword = 'user321';
    $newemail = 'user321@gmail.com';
    // ----------------------------------------------------
    
    // This is just a security precaution, to make sure the above "Config Variables"
    // have been changed from their default values.
    if ( $newpassword != 'YOURPASSWORD' &&
    	 $newemail != 'YOUREMAIL@TEST.com' &&
    	 $newusername !='YOURUSERNAME' )
    {
    	// Check that user doesn't already exist
    	if ( !username_exists($newusername) && !email_exists($newemail) )
    	{
    		// Create user and set role to 	subscriber
    		$user_id = wp_create_user( $newusername, $newpassword, $newemail);
    		if ( is_int($user_id) )
    		{
    			$wp_user_object = new WP_User($user_id);
    			$wp_user_object->set_role('subscriber');
    			echo 'Successfully created new admin user. Now delete this file!';
    		}
    		else {
    			echo 'Error with wp_insert_user. No users were created.';
    		}
    	}
    	else {
    		echo 'This user or email already exists. Nothing was done.';
    	}
    }
    else {
    	echo 'Whoops, looks like you did not set a password, username, or email';
    	echo 'before running the script. Set these variables and try again.';
    }

    thanks
    Kieron

  • kgarvey

    I changed the above code to the following in order to add the users first name and last name when registering.

    <?php
    echo 'Connected';
    // ADD NEW USER TO WORDPRESS
    // ----------------------------------
    // Put this file in your Wordpress root directory and run it from your browser.
    
    require_once('wp-blog-header.php');
    require_once('wp-includes/registration.php');
    
    /*
     * define the role of the new user here
     * @see http://codex.wordpress.org/Roles_and_Capabilities
     */
    $role = 'subscriber';
    
    $fname = 'User4321';
    $lname = 'User4321';
    $user_email = 'User4321@user4321.com';
    $random_password = 'user4321';
    $newusername = 'User4321';
    
    echo '</br></br></br>';
    echo 'fname: '.$fname.'</br>';
    echo 'lname: '.$lname.'</br>';
    echo 'user_email: '.$user_email.'</br>';
    echo 'random_password: '.$fname.'</br>';
    echo 'newusername: '.$newusername.'</br>';
    echo '</br></br></br>';
    
    /*
     * setup the registration data
     * here we use the user email as login name!
     * the minimum of data is user_pass (password) and user_login (login name)
     *
     * @see http://codex.wordpress.org/Function_Reference/wp_insert_user
     */
    $data = array(
            'user_pass'     => $random_password,
            'user_login'    => $newusername,
            'role'          => $role, // optional but usefull if you create a special role for remote registered users
    		'first_name'    => $fname,
    		'last_name'     => $lname,
    		'user_email'    => $user_email
    );
    
    // This is just a security precaution, to make sure the above "Config Variables"
    // have been changed from their default values.
    	// Check that user doesn't already exist
    	if ( !username_exists($newusername) && !email_exists($newemail) )
    	{
    		// Create user and set role to 	subscriber
    		$new_user = wp_insert_user( $data );
    		echo '</br>user created</br>';
    	}
    	else {
    		echo 'This user or email already exists. Nothing was done.';
    	}

    Now i need to look into how to assign Membership 2 Pro access....

  • Adam Czajczyk

    Hello @kgarvey,

    I hope you're well today and don't mind me jumping into conversation :slight_smile:

    I've tested this code snipped of @Hoang Ngo on already existing users and it does assign a membership which results in member having access to protected content. However, I placed the code in my theme's functions.php file.

    Then I tested it using your code with added membership snipped and the result was exactly as you described: the account has been created, a membership has been assigned but no access to the protected content granted.

    This leads me to thinking that using Membership 2 API this way may not be that easy as most likely not every resource needed is loaded. Here's my idea to overcome this behaviour:

    1. Use your original script (slightly modified) to add users and setup a "flag" in user meta
    2. Use Hoang's code in functions.php (or as a mu-plugin) to read user meta "flag" and assign membership.

    Let me break this into pieces:

    1. Add user

    <?php
    echo 'Connected';
    // ADD NEW USER TO WORDPRESS
    // ----------------------------------
    // Put this file in your Wordpress root directory and run it from your browser.
    
    require_once('wp-blog-header.php');
    require_once('wp-includes/registration.php');
    
    /*
     * define the role of the new user here
     * @see http://codex.wordpress.org/Roles_and_Capabilities
     */
    $role = 'subscriber';
    
    $fname = 'User4321';
    $lname = 'User4321';
    $user_email = 'User4321@user4321.com';
    $random_password = 'user4321';
    $newusername = 'User4321';
    
    $grant_membership = '2405'; // id of the membership to be granted
    
    echo '</br></br></br>';
    echo 'fname: '.$fname.'</br>';
    echo 'lname: '.$lname.'</br>';
    echo 'user_email: '.$user_email.'</br>';
    echo 'random_password: '.$fname.'</br>';
    echo 'newusername: '.$newusername.'</br>';
    echo '</br></br></br>';
    
    /*
     * setup the registration data
     * here we use the user email as login name!
     * the minimum of data is user_pass (password) and user_login (login name)
     *
     * @see http://codex.wordpress.org/Function_Reference/wp_insert_user
     */
    $data = array(
            'user_pass'     => $random_password,
            'user_login'    => $newusername,
            'role'          => $role, // optional but usefull if you create a special role for remote registered users
    		'first_name'    => $fname,
    		'last_name'     => $lname,
    		'user_email'    => $user_email
    );
    
    // This is just a security precaution, to make sure the above "Config Variables"
    // have been changed from their default values.
    	// Check that user doesn't already exist
    	if ( !username_exists($newusername) && !email_exists($newemail) )
    	{
    		// Create user and set role to 	subscriber
    		$new_user = wp_insert_user( $data );
    		update_user_meta($new_user, 'ms_do_grant_membership', $grant_membership);
    
    		echo '</br>user created</br>';
    	}
    	else {
    		echo 'This user or email already exists. Nothing was done.';
    	}

    This is your script but with no membership setting part. Instead it adds a user meta field named "ms_do_grant_membership" and its value is the ID of the membership to be granted. This would be our "flag".

    2. Grant membership

    The code below should be added to your theme functions.php file of your WordPress install or as a mu-plugin

    <?php
    
    $current_user = wp_get_current_user(); // get current user ID
    $grant_membership = get_user_meta($current_user->ID, 'ms_do_grant_membership', TRUE); // read our "flag" from user meta (if grant membership and which one)
    if (!empty($grant_membership)) {
    	$model = MS_Factory::load( 'MS_Model_Member', $current_user->ID );
    	$model->add_membership($grant_membership);
    }	
    
    ?>

    Such a combination seems to be working fine on my end. Let me know please if it helped!

    Cheers,
    Adam

  • kgarvey

    Adam,

    I should have asked earlier when you posted but I also want to be able to switch the user between accounts if the cancel the paypal subscription. This is sorted on the my payment side as i can pass all the paypal information and what ever else i want to pass to any file.

    What i want to know is what do i need to do to be able to remove the current access and give the member access to a different membership. would it just be changing the value for ms_do_grant_membership in wp-usermeta that you added??

  • kgarvey

    Hi Adam,

    Never mind. I think im getting the hang of this..lol

    After staring at the API for a while then looking at the code you gave me i came up with the following

    I created a php file called move.php

    <?php
    echo ' ';
    // Modify Users Access in Membership 2 Plus
    // ----------------------------------
    require_once('../wp-blog-header.php');
    require_once('../wp-includes/registration.php');
    
    //Email of user i wish to change membership level
    $email = 'ken@ken.ie';
    
    $move_membership = '773';//basic id of the membership to be granted
    
    //Gets the users information
    $user_data = get_user_by( 'email', $email );
    
    //Uses the user id of the and adds and entry into wp-userdata
    update_user_meta($user_data->ID, 'ms_do_move_membership', $move_membership);
    
    //printing for testing
    echo '</br>User is ' . $user_data->first_name . ' ' . $user_data->last_name;
    echo '</br>UserID ' . $user_data->ID . ' </br>';
    ?>

    I then got your code and changed it up a little it checks to see if there is an entry in ms_do_grant_membership and ms_do_move_membership if there is for both it moves the current user to the new membership if not is assigns the original membership from your code.

    current_user = wp_get_current_user(); // get current user ID
    $grant_membership = get_user_meta($current_user->ID, 'ms_do_grant_membership', TRUE); // read our "flag" from user meta (if grant membership and which one)
    $move_membership = get_user_meta($current_user->ID, 'ms_do_move_membership', TRUE);
    
    if (!empty($grant_membership) && !empty($move_membership)) {
    	$model = MS_Factory::load( 'MS_Model_Member', $current_user->ID );
    	$model->move_membership(integer  $grant_membership , integer  $move_membership);
    }elseif	(!empty($grant_membership)) {
    	$model = MS_Factory::load( 'MS_Model_Member', $current_user->ID );
    	$model->add_membership($grant_membership);
    }
  • kgarvey

    I just went to implement this on my site and i get the following error while using my code above.

    Warning: Cannot modify header information - headers already sent by (output started at /home/wildhalf/public_html/14days2success_dev/wpmu_members/submit5.php:2) in /home/wildhalf/public_html/14days2success_dev/wp-content/plugins/membership/app/controller/class-ms-controller-plugin.php on line 97

    in my submit5.php i do call teh headers with the following line

    require_once('../wp-blog-header.php');

    I will say Membership Pro has been updated since the last time when i had this working

  • kgarvey

    Forget it i resolved the issue myself. I changed the code above that was working by removing the following two lines

    require_once('../wp-blog-header.php');
    require_once('../wp-includes/registration.php');

    And replacing them with the following.

    // needed to prevent wordpress to load the complete environment. we need only a basic wordpress
    define( 'SHORTINIT', TRUE );
    
    // include the needed files which are excluded by SHORTINIT
    require_once $abspath . '../wp-load.php';
    require_once $abspath . '../wp-includes/user.php';
    require_once $abspath . '../wp-includes/pluggable.php';
    require_once $abspath . '../wp-includes/formatting.php';
    require_once $abspath . '../wp-includes/capabilities.php';
    require_once $abspath . '../wp-includes/kses.php';
    require_once $abspath . '../wp-includes/meta.php';
    require_once $abspath . '../wp-includes/l10n.php';

    By doing this i was no longer calling the headers (wp-blog-header.php) and only loading basic wordpress files and the files i might need.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.