• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2764

Original Genesis Tutorials & 6000+ Guaranteed Code

Snippets

  • Premium Access
  • Log in

Delete Posts For Expired Subscribers When Role Changes

This tutorial provides the code which enables you to delete all posts ( and change capabilities ) for any user role if the subscribers role changes to customer.

The code has been written for a member based on this question :

I am using WooCommerce subscriptions. And members are able to create an entry to a custom post type from the front end if they have an active subscription. That works fine. However when they do not renew their subscription, their post remains on the site and we need to manually remove the post.The post can’t remain as the value of the subscription is in being able to have the post on the site (essentially, an entry in a directory on the front end). So I need a code which works along the lines of… If a subscriber’s subscription expires, THEN remove the post by that subscriber in custom post type “author”. When a person becomes a subscriber, they are given a new user role in WP “subscriber”. When their subscription expires they are reverted back to a standard role (called “customer”).

Video Demo

Shows how the subscriber can publish posts which are deleted when the subscriber role is changed to the customer role with read only capability.

Step 1: Here’s the 1st of 3 code snippets you can copy & paste to the end of your child themes functions file.

The code above assumes you are using WooCommerce with the subscription extension however if you’re not, you can use the following code to modify the default capabilities for a subscriber as well as add a new role for a customer. Also note, the code assumes use of the standard post_type post. Change the value for post_type for use with any custom post type.

Step 2:

The code above create a new role named customer with the read capability only.

Step 3 :

Register for full access

The code above modifies the default subscriber role so subscribers can edit, publish and delete their own posts.

Note : All posts will be deleted ( and saved in trash for 30 days ) as soon as the role changes to customer. Once the user role has changes to customer, the user will be allowed read only access. However, if the user changes back to a subscriber role, their posts can be restored from trash within a specific time frame. If you want to force delete, add a 2nd parameter true to wp_delete_post

Move Posts To Draft When Subscription Expires

If you would rather keep all users posts and move them to draft instead of delete them, use this code.

Roles & Capabilities WooCommerce

Reader Interactions

Comments

  1. Ellie says

    January 20, 2018 at 9:39 am

    Hi I have tried to use this but can not get it working. I have even tried this with WP default roles

    can you assist please

    Log in to Reply
    • Brad Dalton says

      January 20, 2018 at 8:50 pm

      Works as shown in the demo video https://youtu.be/rNhjUirS-74

      Coding support is provided for registered users.

      Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags