1. Home
  2. Knowledge Base
  3. WooCommerce Members Only
  4. Roles
  5. Add new user role when existing role expires
  1. Home
  2. Knowledge Base
  3. WooCommerce Members Only
  4. Add new user role when existing role expires

Add new user role when existing role expires

If you are setting an expiry time on membership roles, you might want to add a new role when the membership role expires.

You can do this with the following snippet. Note that if you are assigning a new role to replace an expired role, you will need to have created that new role in WooCommerce > Settings > Members Only > Roles.

<?php
/**
* Add new user role when existing role expires
* Note that the new role will need to be created first
*/
function prefix_after_expired_user_remove_role( $user, $role ) {
// This will add the 'Expired' user role.
// Ensure that you have already created this user role in WooCommerce > Settings > Members Only > User Roles
$user->add_role( 'expired' );
}
add_action( 'wcmo_after_expired_user_remove_role', 'prefix_after_expired_user_remove_role', 10, 2 );

Here’s how to add a snippet.

Was this article helpful?

Related Articles