Discover in this tutorial how to display Stripe’s active subscribers broken down by membership level using its API and a WordPress shortcode.
If you or any of your clients have an online business that uses Stripe as a payment gateway and is a lover of Open Metrics, this will surely interest you.
In a previous tutorial you saw how to display the number of active subscribers you have in Stripe using their fantastic API, and keep it always updated wherever you want on the web
What if you have several membership levels and you would like the numbers to be displayed in separated way?
If this is your case, here’s how to do it:
Instructions to create a shortcode that displays the number of active Stripe subscribers broken down by membership level
1. Create the shorcode
Add the following code at the end of functions.php or in your functionality plugin:
In this code you use the Stripe library and the secret API key stored by Restrict Content Pro.
If you are using another plugin (Easy Digital Downloads, Gravity Forms…) you would have to change that part and adapt it. If you want me to prepare a tutorial with some variation just let me know in the comments.
Stripe API does not have an endpoint where you can get the total number of active subscribers per membership level, so you have to get all of them (using a loop with the function
autoPagingIterator), and add them up.
It is worth noting that Stripe subscriptions can have several statuses:
- active = active
- trialing = testing
- canceled = canceled (but not expired)
- incomplete = incomplete payment (retry)
- incomplete_expired = incomplete payment (not retried)
- past_due = when payment date has passed
- unpaid = not paid
In this case, you are counting the first 3, but if you see that there is a discrepancy with your Stripe dashboard it is because it also takes past_due into account. If you want to include them just add
|| $subscriber->status === 'past_due' to the conditional.
In addition, you get the identifiers of the subscription levels created in Stripe
$subscription_level = $subscriber->plan->id; to be able to display the data broken down.
Depending on the project, this request can be quite “heavy”, so inside the shortcode you create and use a transient to cache the result for a day (although you can modify it to your liking). This way you make sure that the result is displayed immediately.
2. Insert the shortcode where you want
Finally, just type
[active_subscribers_per_level] where you want it to be displayed.
Once the shortcode is rendered it will display a list with the name of the membership levels followed by the number of their active subscriptions. For example:
- Level 1: 70
- Level 2: 30
- Level 3: 20
Now you know how to display the active subscribers number in Stripe broken down by membership levels and that this data is always kept updated at any point of the website using a shortcode.
Even if you are not going to use this snippet to display the data publicly, you can use it to create a dashboard where you can add all the relevant data of your business.
In addition, with small variations you can customize it to fit 100% to your needs.
Now you just have to put it into practice. 😉
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. 🙂