Please note that this documentation is for expert developers only. You will need to hire our developer if you need any help with this.
Simple Membership plugin has an API Addon that allows you to update a member’s account using a standard HTTP POST request.
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 Update a Profile
To update an user account, it’s required to make POST request to your WP install’s home URL (example: https://example.com)
Required parameters to access the API are:
- swpm_api_action = update
- key = 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 (using the input parameter “password”). The addon will handle the rest by itself.
Minimum Required User Info
Minimum required user info parameter to update a user is the following:
- member_id
Note: You can’t update the ‘user_name’ field.
Code Example
The following code example shows how to update a user with minimum parameters using cURL:
$post_arr = array(
'swpm_api_action' => 'update',
'key' => '8c44c3a0b5aa49ab45d82af4acb42149',
'member_id' => '36',
'first_name' => 'John',
'last_name' => 'Smith',
'email' => 'johns@example.com',
'password' => 'testpass0998',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://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 updated 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 updated 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"}