Please note that this documentation is for expert developers only. You will need to hire our developer if you need any help with this.
The Simple Membership plugin has an API Addon that enables certain remote actions via standard HTTP requests. You can use that addon to create a member profile via an HTTP request.
Table of Contents
Ensure the API Addon is Enabled
If you haven’t done so already, visit the SWPM API addon page to download it and then enable it in the settings.
Enabling the API
When the Simple Membership API addon is active, you can access the settings of this addon from the following interface:
Simple Membership -> Settings -> Addons Settings -> API Addon Settings
Enable the API and take note of the API key so you can use it.
Using the API to Create a Member Profile
To create a new user, you need to send an HTTP POST request to the home URL of your WordPress installation (example: https://simple-membership-plugin.com).
Note: To create a user, you must use an HTTP POST request; a GET request is not applicable.
Required parameters to access the API are:
- swpm_api_action=create
- key=<the-api-key>
Parameter names are equal to members_tbl column names and format. Example: if you want to set company name for the user, you just pass company_name parameter:
company_name = Imaginary Company Ltd.
To set a password for user, you just need to provide it in plain text. The addon will handle the rest by itself.
Minimum Required User Info
Minimum required user info parameters to create a user are the following:
- first_name
- last_name
Optional Additional Parameter
You can specify the following optional parameters in the request:
- user_name
- password
- membership_level
- send_email
The additional parameter send_email can be 1 or 0. If it’s 1, addon will send registration email to user upon successful user creation. If not – it won’t. If this parameter is omitted, core plugin setting ‘enable-notification-after-manual-user-add’ will be used to determine if email needs to be sent or not. Also note that sending email may delay API response since there is some time required for WP to send the email.
Code Example for Creating a Profile
Note: The ‘examples’ folder within the addon contains sample code for your reference.
The following code example shows how to create a user with minimum parameters using cURL. After execution, it will send the user an email and prompt him to complete the registration:
$post_arr = array(
'swpm_api_action' => 'create',
'key' => '8c44c3a0b5aa49ab45d82af4acb42149',
'first_name' => 'John',
'last_name' => 'Smith',
'email' => 'johns@example.com',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.example.com/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_USERAGENT,'curl');
curl_setopt($ch, CURLOPT_POSTFIELDS,
$post_arr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
curl_close ($ch);
$res=json_decode($response,true);
if ($res!==NULL) {
var_dump($res);
} else {
//API returned unexpected result
echo "Error occurred";
}
API Response
You get JSON string similar to the following:
{"result":"success"},"message":"Member created successfully","member":{"first_name":"John","last_name":"Smith","email":"'johns@example.com","company_name":null,"account_state":"active","membership_level":2,"reg_code":"62c5220bf960bf379b908b4190390a88","member_id":36}}
The created user’s info is shown.
If there is an error, the response will be similar to the following:
{{"result":"failure"},"errors":{"email":"Email is already used. (johns@example.com )"},"message":"Errors occurred"}