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

Comments

  1. Awesome.
    Any idea on how to do this in reverse? Specifically, when someone creates an account to assign their user role as “customer”? And also- when someone is officially approved to become a vendor to switch their user role to “Vendor”?

    I’m using a plugin to hide particular menu items based on user role, but EDD doesn’t auto assign the appropriate roles…

    Thanks!! 🙂

    • Hey David,

      You’d need to look for the actions to hook in to. Depending what you’re doing to create the account and what you’re doing to approve them as a vendor. Find out what actions are fired when those things happen, then hook in to them using the code above (modified with the correct hook).

      Is your account creation and vendor approval functionality all baked-in with Easy Digital Downloads?

      Matt

  2. Hey Matt,

    Could you elaborate how you created an “application form that accepts the payment, creates the EDD subscription, creates the user account and applies the ‘Field Staff’ user role automatically”.

    Thank you!

    • Hey Matt, Originally, I had set up a Gravity Form using the EDD add-on. It had some trouble with recurring payments setup so the way it currently works is this: 1. User fills out application (Gravity Form). 2. Admin reviews and if approved triggers an approval email. The approval email includes a link that takes them directly to the EDD cart with the appropriate EDD item already in the card. 3. The cart is set so they must create an account when checking out so they create their user account during purchase of the EDD item. 4. I have a function that modifies user role when a subscription is purchases (see below).

      //Set Field Staff User Role
      function wd_edd_run_when_purchase_complete( $payment_id, $new_status, $old_status ) {
      
          // Make sure that payments are only completed once
          if( $old_status == 'publish' || $old_status == 'complete' ) {
              return;
          }
      
          // Make sure the payment completion is only processed when new status is complete
          if( $new_status != 'publish' && $new_status != 'complete' ){
              return;
          }
      
          $payment_data = edd_get_payment_meta( $payment_id );
          $user_info = maybe_unserialize( $payment_data['user_info'] );
          $currentUser = $user_info['id'];
      
          $user = new WP_User( $user_info['id'] );
          $user->set_role( 'fieldstaff' );
      
      }
      add_action( 'edd_update_payment_status', 'wd_edd_run_when_purchase_complete', 100, 3 );
      
      • Thank you for the reply! Quick question, are the approval emails unique to a user/submission? How do you prevent the link with the EDD item already in the cart from others?

        When you say the admin “reviews” and trigger an approval. Through what method are you doing that?

        Something like: https://gravitywiz.com/simple-submission-approval-gravity-forms/
        or: https://wordpress.org/plugins/gravityformsapprovals/

        Your suggestions are very helpful, thank you again!

        • Technically, nothing would prevent other users from getting the products, but the EDD product has a 301 redirect on it so nobody can access it. So for someone to get it to their cart they would need to know the product ID, variation ID and know how to use add to cart EDD urls to figure it out. Long shot there…so if something figures that out, more power to them!

          The client has a canned email setup in his G-Suite account. So he reads over the application and if approved, used a canned email, swaps out the email and name, and sends it out.

          If all users will be approved regardless you can use something like a delayed Gravity Forms notification to trigger the notification after a set amount of time. That add-on is available by request from Gravity Wiz.

Leave a Reply

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