Delete Instead of Demote Based on Role

Good morning everyone,

I am working on changing the Automatic EOT process and looking to see if anyone has done something similar.

Right now, our EOT process is that when a user’s payment fails or their EOT is complete, they are demoted and sent to subscribers level (#0).

What I am wondering is possible is to have one specific level - Level 8 - be deleted instead of demoted when the payments or EOT fails. I’ve searched around and the only similar KB article is on
specifying demotion role upon EOT: https://s2member.com/kb-article/how-do-i-specify-the-demotion-role-upon-eot/

Does anyone have any suggestions? I’m still relatively new to MU Plugins and any advice would be welcome.

Thank you very much.

Emma, try adding this to your site as an mu-plugin:

<?php
function kts_delete_specific_role_on_eot( $user_id, $old_user_data ) {
	// get old user info
	$old_roles = $old_user_data->roles;
	 	
	// get new user info
	$user_info = get_userdata( $user_id );
	$new_roles = $user_info->roles;
	
	// delete user on EOT
	if( in_array( 's2member_level8', $old_roles ) && in_array( 'subscriber', $new_roles ) ) {
		wp_delete_user( $user_id );
	}
}
add_action( 'profile_update', 'kts_delete_specific_role_on_eot', 10, 2 );

I haven’t tested it, so please try it on a test account before using it for real.

Hi Tim,

I will give it a test to make sure but, in the meantime, thank you! Will report back if it works.

Thank you again,
~Emma