Found Art is a series where we take real code from the wild (usually from open source projects) and use it to demonstrate different programming techniques in WordPress.

Chapter 10 of Building Web Apps with WordPress is all about the built in XMLRPC API for WordPress. We didn’t have time to get to it in the book, but WordPress makes it very easy to add your own methods to be called through the XMLRPC API.

A good example of this are the methods added by Paid Memberships Pro which allow API users to query for the membership level for a particular user and also to test if a user has access to a particular post. These calls have been used to develop single sign on solutions between a PMPro-powered WordPress site and other non-WordPress apps.

All of this code can be found in the /includes/xmlrpc.php file of the Paid Memberships Pro plugin. I’ve also broken out the code into sections so you can browse it here.

First, we add our methods to the WordPress XMLRPC API. Each method points to a callback function to handle it.

Then we write the callback function for the pmpro.getMembershipLevelForUser method call.

And finally, the callback function for the pmpro.hasMembershipAccess method call.

Notice that each callback function has a single parameter $args passed into it, which is an array of all of the arguments passed to the method via the XMLRPC call. The first two arguments will always be the username and password being used to access the API. The other parameters will come after. You should document and expect the arguments to be passed into the method in a certain order. In the pmpro.getMembershipLevelForUser method, the 3rd ($args[2]) parameter is the ID of the user to get a membership level for.

The actually callback function pmpro_xmlrpc_getMembershipLevelForUser() simply checks that the user calling is an admin or has the pmpro_xmlrpc capability and then passes the 3rd user_id parameter to the pmpro_getMembershipLevelForUser() function and returns the result, which is going to be serialized and delivered via the API. In this case, the pmpro_getMembershipLevelForUser() will return false for non-members and an object containing the membership level data for members.

With this API in place, you can make a call to the method using code like this in a WordPress app. If you place this code into a custom plugin or your theme’s functions.php file, it will load the XMLRPC class bundled with WordPress and make a call to a remote site where Paid Memberships Pro is activated and the PMPro methods are available. To test this out, make sure you update the remote url, username, password, and remote_user_id in the sample code and then navigate to “/?test=1” on the site you installed the code on. Notice that this is just a test. In the real world, this code would be part of a larger plugin or app and you would obviously do something with the level data returned instead of just halting output.

I hope this shows how easy it is to add your own API methods to WordPress. I know I was very surprised at how quickly I was able to add the method to Paid Memberships Pro when I finally got down to it.

If you want to see more examples like this, with great explanation of the inner workings of WordPress, be sure to pick up our book Building Web Apps with WordPress.

Article by


One Response to Found Art: Add Your Own XMLRPC API Methods to WordPress

  1. Interacting with Paid Memberships Pro Through APIs | Paid Memberships Pro says:

    […] 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/ […]

Leave a Reply

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