REST API for Membership Pro 2

Hi, I am developing mobile app (android) for my wordpress page.
I am using plugin wordpress rest api for viewing my website on mobile apps, but I want to know how can i get the membership plugin on this api because the rest api cannot get the membership plugin api.

User with membership role cannot access the content on mobile that been protect by membership on website.

Thanks.

  • James Morris

    Hello fairos,

    I hope you are well today.

    Since this is dealing with more advanced coding topics, I'm going to ping our SLS Team (code experts) to get their valuable feedback on this issue. One of our SLS members will reply back here as soon as more information is available.

    Please keep in mind that our SLS Team deals with more complicated issues, thus it may take a little longer for them to reply here. Thank you for your patience while we look into this further.

    Best regards,

    James Morris

  • Lindeni Mahlalela

    Hello Fairos,

    I hope you are having a great day so far and I am very sorry for the late reply.

    The Membership 2 Pro API was built to expose the functionality of the membership plugin to other plugins within the rest WordPress system. On the other hand the WordPress REST API was built mainly for WordPress core functionality.

    The good news is that you can extend the WordPress REST API by adding custom endpoints that will in turn call the functionality of Membership 2 Pro API. In order for you to achieve your goal, you have to create a plugin that will bridge the gap between the APIs of Membership 2 and the WP Rest API.

    You will have to have a fair knowledge of PHP and how WordPress plugins work in order to create the functionality you need. This should not be difficult if you have good knowledge of PHP

    Here are the steps that will help you achieve your goals

    1. Create a plugin that will call the functionality of the Membership 2 API and return the results as you want them. Here is a guide on how to create a plugin . Once you have you basic plugin,

    2. Add functions that will call the Membership 2 functions and return the data in format you prefer like JSON or XML depending on how you use the rest API. Here is the documentation on how to get started with the Membership API and these are the functions you can call for interacting with the member object.

    3. Create end points for the REST API that you will call to access the functionality you created in step 1 and 2. See the Documentation on how to add custom endpoints to the rest API.

    Now you can use the REST API to get data returned by the plugin you created. In this way you can call all the functionality of Membership 2 via the REST API and get the data on your Android App. Unfortunately I do not know how you are implementing your app so I do not have example Android code.

    To get you started, here is a sample plugin I have put together following the documentations above. This is just a rough example that gets the username of a Membership 2 Pro member and return it via the WP Rest API Endpoint. Please note I have not tested this but I created it so that you can get started with the integration and have an idea of what to do.

    Copy the code to a file call it anything like 'membership2-rest-api.php' save it in a folder inside your /plugins folder on your website:

    /wp-content/plugins/m2res-api/membership2-rest-api.php

    Paste the following code in that file and save.

    <?php
    	/*
    	Plugin Name: M2 to WP REST API
    	Plugin URI: http://premium.wpmudev.org
    	Description: Create M2 endpoints for WP Rest API
    	Version: 1.0
    	Author: wpmudev
    	Author URI: http://premium.mpudev.org
    	License: GPL2
    	*/
    
    	class M2_Rest_Endpoints {
    		protected $api = null;
    
    		// Function is always executed. Will create 1 Implementation object.
    		static public function setup() {
    			static $Inst = null;
    			if ( null === $Inst ) {
    				$Inst = new M2_Rest_Endpoints();
    			}
    		}
    
    		// Function set up the api hook.
    		private function __construct() {
    			add_action( 'ms_init', array( $this, 'init' ) );
    			add_action( 'rest_api_init', function () {
    			  	register_rest_route(
    					'membership/v1', '/username/(?P<id>\d+)', array('methods' => 'GET', 'callback' => array($this,'get_username') )
    				);
    			});
    		}
    
    		// Function is only run when Membership2 is present + active.
    		public function init( $api ) {
    			$this->api = $api;
    			// The main init code should come here now!
    		}
    
    		//sample function to get username
    		function get_username( $data ) {
    			return $this->api->get_username($data['id']);
    		}
    
    		// Add other event handlers and helper functions.
    		// You can use $this->api in other functions to access the API object.
    	}
    	M2_Rest_Endpoints::setup();
    ?>

    Activate the plugin by going to your WordPress dashboard on the page "Plugins > Installed Plugins" it should be called 'M2 to WP REST API', click activate and try to access the endpoint as follows

    <yourwebsite>/membership/v1/username/<user_id>

    It should return the username of the user with that ID. Please note that I have not tested as I do not have an App to test with but that is basically how you should go about creating the endpoints and unlocking the content for members. Also my example was to basic but will help you understand how you should do this.

    I hope this helps you achieve your goals. If you are not familiar with WordPress development you can hire someone with the skills and get them to do the WordPress part while you do the Android part of the project. If you cannot do this by yourself then you may get help by hiring a professional WordPress developer on the Jobs and Pros website.

    I hope you enjoy your day further.

    Cheers,
    Mahlamusa