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

WP SITES

2662

Original Genesis Tutorials & 5000+ Guaranteed Code

Snippets

  • Support
  • Newsletter
  • Videos
  • Log in

Premium Member? - Request custom code

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.

Here’s the code you can copy & paste to the end of your child themes functions file.

add_action( 'set_user_role', 'delete_customer_posts', 10, 2 );
function delete_customer_posts( $user_id, $role ) {

    if ( 'customer' == $role ) {
        
    $args = array(
        'numberposts' => -1,
        'author' => $user_id,
        'post_type' => 'post',
              
    );
    
    $expired_member_posts = get_posts( $args );
    foreach( $expired_member_posts as $postid ) {
            
    wp_delete_post( $postid->ID );
        
         }
     }
}

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.

add_action( 'admin_init', 'add_customer_user_role' );
function add_customer_user_role() {

add_role(
    'customer',
    __( 'Customer - Expired Subscriber' ),
    array(
        'read' => true,  

    ));  

}

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

add_action( 'admin_init', 'add_subscriber_capabilities');
function add_subscriber_capabilities() {
 
    $role = get_role( 'subscriber' );

    $role->add_cap( 'edit_posts' ); 
    $role->add_cap( 'edit_published_posts' );
    $role->add_cap( 'publish_posts' ); 
    $role->add_cap( 'delete_posts' ); 
    $role->add_cap( 'delete_published_posts' ); 
}

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

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

    add_action( 'set_user_role', 'delete_customer_posts', 10, 2 );
    function delete_customer_posts( $user_id, $role ) {
    
        if ( 'editor' == $role ) {
            
        $args = array(
            'numberposts' => -1,
            'author' => $user_id,
            'post_type' => 'menus',
    		
                  
        );
        
        $expired_member_posts = get_posts( $args );
        foreach( $expired_member_posts as $postid ) {
                
        wp_delete_post( $postid->ID );
            
             }
         }
    }

    can you assist please

    Log in to Reply
    • Brad Dalton says

      January 20, 2018 at 8:50 pm

      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

PHP Code

template_include

get_body_class

if else

array

class_exists

foreach

sprintf

add_action

printf

variable

Advertise · WPEngine · Genesis · Log in

  • How Premium Membership Works
  • Sign Up
  • Support
  • Subscription Details/Invoice
  • Tagged Tutorials
  • Access-Download Problems