Discover in this tutorial how to calculate and display subscriber loyalty months in Restrict Content Pro.
If you have a membership created with Restrict Content Pro you may be interested in rewarding the most loyal subscribers. But the plugin does not show a default age or loyalty value.
Do you want to know he best way to calculate it?
activated_date value which records the date when the first payment is made and the membership is activated.
As you can’t get this value from the plugin’s predefined functions, the first thing you need to do is to implement the tutorial to access a value from the WordPress database.
Once you get the data, you will calculate the loyalty time in months and display it in the “Membership details” section for each subscriber.
Shall we get to it?
Steps to display a subscriber’s loyalty months in Restrict Content Pro
1. Generate a new metadata for users
The first thing you are going to do is create a metadata associated with the users that stores the value of how many months they have been subscribed to your membership
If you use this example to the letter you will call it “loyalty”.
Add the following code at the end of functions.php or in your functionality plugin:
With this snippet you use the Restrict Content Pro hook
rcp_membership_post_activate to create a new meta field called “loyalty” associated with the user.
This hook is executed after the membership is created and the first payment is made.
As you can see, you start it with the value 0 since this will be the number of months it has been running at that moment.
2. Calculate the number of months of seniority of a Restrict Content Pro subscriber
Now you are going to apply what you learned in the previous tutorial to access the
activated_date value that Restrict Content Pro plugin creates when the first payment is made and the membership is activated.
With this fragment you get:
- Get the ID of the user associated with that membership.
- Access the
activated_datevalue of the user membership.
- Calculate the loyalty months using PHP calculations.
- Update the “loyalty” metadata with the new number of loyalty months.
Now, every time the subscription is renewed it will run the hook
rcp_membership_post_renew and the time in months since the start of the subscription will be calculated.
3. Display the value in the membership tab
Once you have calculated and saved the subscriber’s length of loyalty in the database, all that’s left is to display it in the Memberships section of Restrict Content Pro.
Thanks to this snippet a new field will appear in the “Membership Details” section of each subscriber (under Restrict > Membership):
This allows you to quickly visualize the subscriber’s loyalty level quickly.
But, in addition to this, you can use the value of the “loyalty” metadata stored in the DB to give exclusive access to content depending on how long your users have been subscribed by combining it with the
Do you want to quickly see the loyalty of Restrict Content Pro subscribers?
Now you know how to calculate and save the age of your users in a new metadata in the usermeta table.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂