Modify User Role When Easy Digital Downloads (EDD) Subscription is Cancelled

In this tutorial I will show you how to modify the WordPress User Role when an Easy Digital Downloads (EDD) subscription is cancelled. Note that this assumes you are using Easy Digital Downloads with the Recurring Payments extension.

Here is a little information about my specific case use for this:

  • I created a custom WordPress User Role called ‘Field Staff’ (slug -> fieldstaff)
  • I have an application form that accepts the payment, creates the EDD subscription, creates the user account and applies the ‘Field Staff’ user role automatically
  • The ‘Field Staff’ user role gives the subscriber access to specific parts of the site

So, when the user cancels their subscription their WordPress User Role needs to immediately change, which in-turn revokes their access. So, with that said, the function below will automatically change the user’s role to ‘Subscriber’ when they cancel their recurring subscription. This snippet should be added to your functions.php file.

<?php
//Downgrade user role when field staff cancel subscription
function wd_downgrade_fieldstaff( $sub_id, $subscription ) {
	$u = new WP_User( $subscription->customer->user_id );
	$u->remove_role( 'fieldstaff' );
	$u->add_role( 'subscriber' );
}
add_action( 'edd_subscription_cancelled', 'wd_downgrade_fieldstaff', 10, 2 );

What this function does is it finds the User that is linked to the subscription that is being cancelled and removes the ‘Field Staff’ role, then adds the default ‘Subscriber’ role. You will want to make sure you change the “fieldstaff” text to whatever role you want removed. Then the edd_subscription_cancelled action ensures that this function runs when an Easy Digital Downloads subscription is being cancelled.

This simple little function will save the end user a lot of time by removing the tedius task of modifying user role each time someone cancels their subscription.

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *