Is it possible to use a function in functions.php that would automatically change user role when a membership goes from status "active" to "expired" ?
For example : if an active membership switches to expired, I want the user role to change automatically from "Subscriber" to "Blocked" for the concerned user.
I've tried the following hook but it's not doing anything when I update the user membership to expired. I think my problem comes from the meta data "expired" which is registered on table "rcp_membership" (field "status") and not on "usermeta" table.
add_action( 'profile_update', 'my_profile_update', 10, 2 );
function my_profile_update( $user_id, $old_user_data ) {
if( get_the_author_meta( "rcp_status", $user_id ) == "expired") {
$u = new WP_User( $user_id );
$u->set_role('blocked');
}
}
And data I have to update (what I try through function "set_role()" in the code) is in the database table "metadata", with this SQL request :
UPDATE `wp_usermeta` SET `meta_value` = 'a:1:{s:11:\"bbp_blocked\";b:1;}' WHERE `wp_usermeta`.`user_id` = 29;