Since version 1.7.8 of PMPro, we have offered two API methods accessible through the WordPress XMLRPC API. This article will describe how to use them and show some examples.


pmpro.getMembershipLevelForUser

Arguments: username, password, user_id
Returns: False for non-members, or an array containing the membership level values for members
Notes: The username and password args should be for an admin user or user given the “pmpro_xmlrpc” capability.


pmpro.hasMembershipAccess

Arguments: username, password, post_id, user_id, return_membership_levels
Returns: Checks if user user_id has access to post post_id and returns true or false. If return_membership_levels is set to true then returns an array whose first value is true or false based on access and the second value is an array of membership levels that have access to that post.
Notes: The username and password args should be for an admin user or user given the “pmpro_xmlrpc” capability.

These methods are defined in the includes/xmlrpc.php file of the plugin.


Sample Using the PMPro XMLRPC Methods

This example can be loaded into the codebase of a separate WordPress site and will use the XMLRPC client bundled with WordPress to access the membership level data for a user on a different system. This is a bare bones example. A real life example might use the wp.getUser method built into WordPress to get the user ID of a user based on a given email address and then use that user ID in the call to pmpro.getMembershipLevelForUser.

You can write a similar call for communicating between your WordPress site and a .NET environment, an IOS or Android App or more. Just find the documentation for making XMLRPC calls on your platform.


How to Build a Simple RESTful API to get PMPro Data

An alternative to using the XMLRPC API is to build simple RESTful scripts to return specific data when specific parameters are passed into a WordPress URL. This method is less secure since there is no user authentication, but it also doesn’t require you to save a username and password in plaintext in a different system that might be used to access parts of your WordPress system you didn’t intend to make public. In general, more measures should be made to make these APIs secure when using them on live sites.

Here is an example that can be added to a client WordPress site’s functions.php or a custom plugin so that when you visit /?verify=email@domain.com&secret=SOMESECRETHERE on the site a JSON encoded arary of membership level data is returned for that user specified. This is very similar to the pmpro.getMembershipLevelForUser XMLRPC example above.

For information on how to create custom plugins for WordPress see our PMPro Customizations plugin.

For more information on how to build your own XMLPRC methods in WordPress see: http://bwawwp.com/add-your-own-xmlrpc-api-methods-to-wordpress/

For more information on the XML-RPC WordPress API, see: http://codex.wordpress.org/XML-RPC_WordPress_API


This entry was posted in Recipes and tagged . Bookmark the permalink. Last updated:

Comments (7)

Author’s gravatar

One of the benefits of an external service I offer is membership to PMP WordPress. I want my external service to call the api and cancel a membership when my external service is not renewed. Can you steer me in the right direction?

Reply
Author’s gravatar

How are you giving them membership when they sign up for the external service?

We don’t have an endpoint in the WP REST API to cancel/change membership yet, but we could build one out for you. Or if you go the “custom API” route, you could setup your WP site to listen for something like /wp-admin/admin-ajax.php?action=cancel_member&user_id={user_id}&secretkey={something you keep secret}. Use wp_ajax_nopriv_cancel_member hook, check for the user id, check the secret key, then call pmpro_changeMembershipLevel(0, $user_id)

We can help in the member forums if you need further assistance.

Reply
Author’s gravatar

I’d like to create a user from a mobile app client (say someone registers using the mobile app, I’d like to create that user in paid membership pro), which API would I be using? The ones stated here only fetch information about the already existing users.

Reply
Author’s gravatar

It looks like the WordPress REST API allows you to create users: http://v2.wp-api.org/reference/users/ I’m not sure if this is bundled with the part of the REST API that is in the core WP code or if you need to use a plugin to enable that.

Reply
Author’s gravatar

I think Bruce was looking for a way to create a new membership. I’m also interested in this topic. Say we want to take the payment on another platform and create a new membership programmatically? How would one do that>

Leave a Reply

Your email address will not be published. Required fields are marked *