Learn in this tutorial how to add a custom role when a customer buys a certain product in Easy Digital Downloads, using code.
One of the most common options when creating a membership is to use the Restrict Content Pro plugin, in fact it is what I use on this website.
But sometimes, for example, if you also want to sell digital products, Easy Digital Downloads can be a very interesting option.
To adapt EDD to create a membership you need 2 extra addons:
- Recurrent Payments
- Content Restriction
If you are using this combination you will have noticed that EDD grants a specific role to the subscriber called “EDD Subscriber”.
But if you have multiple membership levels, you may be interested in being able to assign a custom role to one of them.
Here’s how to do it:
Snippet to add a custom role when purchasing a certain Easy Digital Downloads product
Add the following code at the end of functions.php or in your functionality plugin:
In this case, you use the
edd_complete_purchase hook to assign the user the role of
edd_susbcriber_gold when the customer purchases a particular product (for example the gold level membership).
In this case the product with identifier equal to 316, but you can modify it to suit your needs.
This can be very practical if you want to grant some specific functionalities to the new role and make sure that only those who have the subscription at a certain level have access to them.
The role assigned in this case is
edd_subscriber_gold but you can also change it to any user role you want. The idea is that you create it first, for this you can follow the tutorial where I explain how to create a custom user role.
As you can see, with a few lines of code you can add a specific user role to the buyers of a particular Easy Digital Downloads product.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉