Title: API Instance
Last modified: August 30, 2016

---

# API Instance

 *  Resolved [mumbomedia](https://wordpress.org/support/users/mumbomedia/)
 * (@mumbomedia)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/api-instance/)
 * Hello WPMU DEV -Team,
 * can you please tell me, how to set up an API-Instance so I can create
    and save
   members and membership programmatically. I think I have to `include` / `require`
   some php-files, but I don’t which I have to.
 * This is what I want to archieve. I have a custom online crm system.
    In this 
   system we have stored customers. Each customer is a member of a so called “Kennziffer”.
   Let’s say this “Kennziffer” is a specific membership instance. I want to syncronize
   these customer data via cronjob with your plugin. As I read from the api reference
   in the Help-section of your plugin, I have to set up `$membership`. But this 
   is the point I couldn’t figured out how to make it.
 * Also could you explain me whats the difference between the app and the
    app_old
   folder?
 * [https://wordpress.org/plugins/membership/](https://wordpress.org/plugins/membership/)

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

 *  [Philipp Stracker](https://wordpress.org/support/users/strackerphil-1/)
 * (@strackerphil-1)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/api-instance/#post-6756551)
 * Hey there 🙂
 * Quick one: app_old is the legacy version of the plugin, this folder will be removed
   in the next few weeks. If you use the new layout then this folder is not used(
   you can even delete app_old on your site)
 * The other part:
    To use the M2 API module, you simply need to make sure that 
   WordPress is fully loaded. You cannot include the plugins PHP files in a non-
   WordPress system to access memberships.
 * To do what you want I suggest to write a small custom plugin that listens to 
   specific ajax requests, then your cron-job can send the user-details to the WordPress
   ajax handler, i.e. to “/wp-admin/admin-ajax.php”
 * I hope the following, simple and untested example helps you to get moving in 
   the right direction 😉
    Thanks, Philipp
 *     ```
       <?php
       /**
        * WP Plugin header...
        *
        * You can use this example by calling an URL like this:
        * your-wp-url.com/wp-admin/admin-ajax?action=crm_sync&data={"do":"add","email":"demo@crm.com","membership":"Kennziffer"}
        */
   
       // Listen to the Ajax action 'crm_sync'
       add_action( 'wp_ajax_nopriv_crm_sync', 'crm_sync' );
   
       // Callback for the ajax action, at this point WP is already loaded, so we can use M2 API.
       function crm_sync() {
         $data = json_decode( $_GET['data'] );
   
         $api = ms_api();
   
         if ( 'add' == $data['do'] ) {
   
           // Find the user-ID from the email; if user does not exist create him.
           $user = get_user_by( 'email', $data['email'] );
           if ( $user ) {
             $user_id = $user->ID;
           } else {
             $user_id = wp_create_user( $data['email'], 'password', $data['email'] );
           }
   
           // Get the M2 Member-object from the user-ID.
           $member = $api->get_member( $user_id );
   
           // Get the membership-ID from the membership name.
           $membership_id = $api->get_membership_id( $data['membership'] );
   
           // Assign the membership to the user.
           $member->add_membership( $membership_id );
         }
         elseif ( 'del' == $data['do'] ) {
           // ...
         }
         elseif ( 'update' == $data['do'] ) {
           // ...
         }
       }
       ```
   
 *  Thread Starter [mumbomedia](https://wordpress.org/support/users/mumbomedia/)
 * (@mumbomedia)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/api-instance/#post-6756552)
 * Thank you, this helps me a lot.
    As I’ve seen from the admin-ajax.php requiring
   the wp-load.php will ensure that WordPress is fully loaded.
 * Now I seen the things more clearly.
    All Membership2 members are WordPress Users
   which doesn’t have any rights for backend functionality such as editing a page/
   post. So am I right, you plugin extends the built-in user management of wordpress?
 * And therefore the email adress has to be unique.
 * So this plugin has exactly the same restriction as the also great newsletter 
   plugin, for which I also write a sync script.
    So I can combine it easily.
 * You made my day.
 *  Thread Starter [mumbomedia](https://wordpress.org/support/users/mumbomedia/)
 * (@mumbomedia)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/api-instance/#post-6756609)
 * Hi Philipp,
 * I think, your suggestion has a good approach.
    If I make an Ajax request for 
   each email address that I find in the sync database, not a maximum execution 
   time errors may occur, since each request has its own execution time. Thus then
   more than 4000 Abonenten are no problem.
 * I’m going to consult the official documentation on line codex.wordpress.org about
   plug-in development and hope that I can use it to develop a particularly faultless
   and fast plugin.
 * Thanks again.
 * For any questions I will come back to you.
 * Kind regards
 * b.o. Alexander Behling
    mumbo jumbo media
 *  Thread Starter [mumbomedia](https://wordpress.org/support/users/mumbomedia/)
 * (@mumbomedia)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/api-instance/#post-6756697)
 * Hi Phillip,
 * hopefully I have understand your example right.
 * The server which holds the data to be sync are making the ajax-calls to the plugin
   I developed?
 * Kind regards
 * b.o. Alexander Behling
    mumbo jumbo media
 *  Thread Starter [mumbomedia](https://wordpress.org/support/users/mumbomedia/)
 * (@mumbomedia)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/api-instance/#post-6756710)
 * Hello again Phillip,
 * now I figured it out by myself.
    I make the ajax calls from the server which 
   holds the data to be imported. The best of this solution is that it isn’t necessary
   that the plugin has to know the creditials to the database of the server which
   holds the data. So this should be more save than if the plugin connects to the
   remote server. At so far my plugin creates/updates members or membership and 
   delete the entire subscriber. Everything works as expected. Now I have to extend
   it so that the plugin also work with another necessary plugin for our customer.
 * Many, many thanks again for the helpful snippet.
    I could implement it nearly
   without changes.
 *  [Goharika](https://wordpress.org/support/users/goharika/)
 * (@goharika)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/api-instance/#post-6756768)
 * I have register route with api, for my membership 2 plugin, Here how it looks
   like
    add_action( ‘init’, ‘add_memberships_to_json_api’, 30 );
 * function add_memberships_to_json_api(){
 * global $wp_post_types;
 * $wp_post_types[‘ms_relationship’]->show_in_rest = true;
 * $wp_post_types[‘ms_relationship’]->rest_base = ‘memberships’;
 * $wp_post_types[‘ms_relationship’]->rest_controller_class = ‘WP_REST_Posts_Controller’;
 * }
 * And now get request returns me empty array, what am I doing wrong? I need to 
   get my memberships not empty array.
    Here is the url where I am working on [http://lc13754376.on-rev.com/restapi/wp-json/wp/v2/memberships](http://lc13754376.on-rev.com/restapi/wp-json/wp/v2/memberships)
   I am going to add even ID parts, to get membership by ID, but need help to continue…
 *  [Goharika](https://wordpress.org/support/users/goharika/)
 * (@goharika)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/api-instance/#post-6756772)
 * Hi Philipp,
    I am working with membership plugin api too. Please can you guide
   me , I need to connect membership plugin with WP REST API, as I am none professional
   in php want to find easiest way to do that.
 * I am looking now in help>API documentation, as you code above there is some functions
   too, that we need include to our plugin to make it work.
 * And now I can’t find way how my request url will look like. Or how can I see 
   users data, as I want get this data in my application to control users from there.
   
   Any help will be useful. Thanks
 *  Thread Starter [mumbomedia](https://wordpress.org/support/users/mumbomedia/)
 * (@mumbomedia)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/api-instance/#post-6756773)
 * Hi Goharika
 * I think you need a custom endpoint
    A how to can be found here [.](http://v2.wp-api.org/extending/adding/)
 * Then to setup a membership api instance you need a call to ms_api():
 * so your function should be
 * function add_memberships_to_json_api(){
    $ms_api = ms_api(); $memberships = $
   ms_api->list_memberships(); return json_encode($memberships); }
 * Then you have to register an endpoint
 * describe on the page I have mentioned above.
 * Hope this helps.
 * ‘I am none professional in php want to find easiest way to do that.’
    – So if
   you don’t understand what you are doing, consider to hire a freelancer. They 
   could be found on envato.com for example.
 * Kind regards
    Alexander

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

The topic ‘API Instance’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/membership_b8cdaa.svg)
 * [Membership 2](https://wordpress.org/plugins/membership/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/membership/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/membership/)
 * [Active Topics](https://wordpress.org/support/plugin/membership/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/membership/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/membership/reviews/)

## Tags

 * [api](https://wordpress.org/support/topic-tag/api/)
 * [Membership2](https://wordpress.org/support/topic-tag/membership2/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 8 replies
 * 3 participants
 * Last reply from: [mumbomedia](https://wordpress.org/support/users/mumbomedia/)
 * Last activity: [10 years, 4 months ago](https://wordpress.org/support/topic/api-instance/#post-6756773)
 * Status: resolved