this is the same function used by [Simon Cossar] (https://meta.discourse.org/u/Simon_Cossar)team see this link
ihave used the same function please need help .i have posted code also see above message
This is my update membershiplevel code
function action_pmpro_after_change_membership_level( $level_id, $user_id, $cancel_level ) {
$group_name = dcpmp_get_level_for_id( $level_id );
if ( is_wp_error( $group_name ) ) {
return null;
}
$groupname = DiscourseUtilities::get_discourse_user( $user_id, false );
// Removes the user.
$result = DiscourseUtilities::remove_user_from_discourse_group( $user_id, $groupname );
if ( ! empty( $result->success ) ) {
// Remove the membership level metadata key.
delete_user_meta( $user_id, "dcpmp_group_{$groupname}" );
}
$result1 = DiscourseUtilities::add_user_to_discourse_group( $user_id, $group_name );
if ( ! empty( $result1->success ) ) {
// If the user has been added to the group, add a metadata key/value pair that can be used later.
add_user_meta( $user_id, "dcpmp_group_{$group_name}", 1, true );
}
return $result;
return $result1;
}
add_action( ‘pmpro_after_change_membership_level’, ‘action_pmpro_after_change_membership_level’, 10, 3 );