Introduction
I'm guessing that you have a membership site and you also have a gamification system in place based on MyCRED, and of course, you want to integrate them and give/take points to or from your users when something happens in MemberPress.
For instance, you may want to award MyCRED points when a user signs up. And in fact, that's the trigger that we'll use for demonstration purposes in this article.
But in order to get this done we'll need to add a bit of custom PHP code to your site. This guide is best suited for advanced WordPress users but if you're an ambitious “power user” and want to give it a shot, go for it! We'll be hanging out in the comments section if you need help. 🙂
Requirements
1 – MemberPress Developer Tools add-on
2 – MemberPress User Roles add-on.
3 – MyCRED WordPress points plugin
Once you have these plugins installed and activated, let's see how to actually integrate them.
Video Tutorial
1 – Configure the WebHooks in MemberPress Developer Tools
Before actually doing anything let's see what WebHooks are.
In an easier manner of speaking it's a letter sent by MemberPress to an address that you are choosing with certain events that can help you trigger some custom functionality. If you want a proper explanation, here is a link to Wikipedia's explanation for WebHooks.
In order to get that done, you need to hover your mouse over the ‘MemberPress' option in the sidebar menu. There you have to click on ‘Developer' as you can see here.
And here you have to add your API route which you will configure later in the plugin that we will create.
As you can see in the screenshot I have checked ‘All Events' so that way when the request comes in we will have ALL of the data we might need from MemberPress. Of course, you can modify the events with whatever you need.
2 – Create our own custom plugin
In order to add our PHP functionality we have to either add it to our theme's functions.php file or to create a plugin for it. In this case, my suggestion would be to create a new plugin just to keep everything clean and organized.
I'm assuming that you already know how to create a WordPress plugin but just in case you don't know, here you have a link to WordPress's codex information about writing plugins. Don't be intimidated, it's really simple!
Here is how my plugin looks:
<?php
/*
Plugin Name: MemberFix MemberPress MyCRED Integration
Plugin URI: https://memberfix.rocks
Description: MemberPress -> MyCRED integration
Version: 1.0
Plugin Author: Sorin Marta
Author URI: https://sorinmarta.com
License: GPLv2 or later
*/
add_role(‘memberfix',__( ‘MemberFix' ), array(‘read'=> true,'edit_posts'=> false));
/*
* Register our routes for our WP-API endpoints
*/
add_action( ‘rest_api_init', ‘tc_webhook_route' );
function tc_webhook_route() {
//localhost/wordpress/memberfix-tc/v1
register_rest_route( ‘memberfix-tc/v1', ‘/go', array(
‘methods' => ‘GET',
‘callback' => ‘tc_webhook',
) );
//localhost/wordpress/memberfix-mp/v1
register_rest_route( ‘memberfix-mp/v1', ‘/go', array(
‘methods' => ‘POST',
‘callback' => ‘mp_webhook',
) );
}
/* Function to handle MemberPress Webhook events */
function mp_webhook($request_data){
$parameters = $request_data->get_params();
if($parameters[“event”]=='member-signup-completed'){
$message = $parameters[“data”][“id”].'| <- MemberPress …| ‘;
$customer_id = $parameters[“data”][“id”];
$customer = get_userdata($customer_id);
$customer_roles = $customer->roles;
if( in_array(“memberfix”, $customer_roles) ){
$message .= ‘in_array(“growth”, $customer_roles)';
mycred_add( ‘user_registration', $customer_id, 5, ‘5 Credits added due to signup' );
}
}
}
Now let's take a look at the code piece by piece and explain everything so that way you will know what it does and will be able to customize it for your own needs.
add_role('memberfix',__( 'MemberFix' ), array('read'=> true,'edit_posts'=> false));
With this piece of code, we add a new user role to our WordPress installation, and the reason for that is because you probably have more memberships and this way we can keep our code organized, we can give points to the user based on their user role.
/*
* Register our routes for our WP-API endpoints
*/
add_action( 'rest_api_init', 'tc_webhook_route' );
function tc_webhook_route() {
//localhost/wordpress/memberfix-tc/v1
register_rest_route( 'memberfix-tc/v1', '/go', array(
'methods' => 'GET',
'callback' => 'tc_webhook',
) );
//localhost/wordpress/memberfix-mp/v1
register_rest_route( 'memberfix-mp/v1', '/go', array(
'methods' => 'POST',
'callback' => 'mp_webhook',
) );
}
As the comment says here we register our API route, here we catch that information that MemberPress is sending through the WebHook, just in case you want more information about how the REST API works in WordPress, here is some documentation on WordPress REST API.
/* Function to handle MemberPress Webhook events */
function mp_webhook($request_data){
$parameters = $request_data->get_params();
if($parameters[“event”]=='member-signup-completed'){
$message = $parameters[“data”][“id”].'| <- MemberPress …| ‘;
$customer_id = $parameters[“data”][“id”];
$customer = get_userdata($customer_id);
$customer_roles = $customer->roles;
if( in_array(“memberfix”, $customer_roles) ){
$message .= ‘in_array(“growth”, $customer_roles)';
mycred_add( ‘user_registration', $customer_id, 5, ‘5 Credits added due to signup' );
}
}
}
Our last piece of code does most of the job that you will probably have to customize for your own use case. In this case, we are looking for event parameter with the name of ‘member-sign-up-completed”. Which means that we are going to trigger the following functionality when a user finished the sign-up process.
Then we just assign the points to the user using the mycred_add() function provided by MyCRED which is documented here.
Now that we know how the code works, let's do our last step.
3 – Set the membership to assign the user role
All we have to do here is to edit our Membership in MemberPress and in the “Advanced” tab to set up our new WordPress Role, here is a screenshot with that:
There are plenty of other use cases for this integration, such as:
- Subscription-based virtual shop
- Upgrade bonuses
- Downgrade bonuses
- Resuming bonuses
…etc.
That about does it for this tutorial. So, please let us know in the comments section if this integration helped you in your use case, if you need help, or if you want to just say hi!
If you'd rather hire us to do this for you, that's cool too. 🙂 Just check out our MemberFix service packages here.
Cheers!
Whooo! Thanks Sorin. This is exactly what I was looking for. Really appreciate you taking the time to document all of this and share it 🙂
You are most welcome, Steve!
What about Buddypress any Mycred Integration any idea how to do?