I found some sort of solution. https://www.s2member.com/kb/roles-caps-via-php/
I made the following code in a separate plugin that I already was using for other template stuff. Maybe someone else can use this as well:
if ( !defined( 'ABSPATH' ) ) exit;
function crowd_change_role_from_xprofile($user_id, $posted_field_ids, $errors) {
if(bp_is_my_profile()){
if (xprofile_get_field_data('Field ID') == "field value" ) {
if(!is_admin() || !is_super_admin || current_user_is("subscriber")){
$user_id = get_current_user_id();
$user = new WP_User($user_id);
$old_user = unserialize(serialize($user));
$new_role = 's2member_level1';
$user->set_role($new_role);
do_action ('ws_plugin__s2member_during_collective_mods',
$user_id, // The user's ID in WordPress?
array(), // For debugging only. Use empty array here.
'dynamic-user-role-change', // Role change flag.
'modification', // A modification flag.
$new_role, // New role.
$user, // New user object instance.
$old_user // Old user object instance.
);
fwrite($fp, xprofile_get_field_data('field ID'));
return;
}
}
elseif (xprofile_get_field_data('field ID') == "Field value" ) {
if (!is_admin() || !is_super_admin || current_user_is("s2member_level1")){
$user_id = get_current_user_id();
$user = new WP_User($user_id);
$old_user = unserialize(serialize($user));
$new_role = 'subscriber';
$user->set_role($new_role);
do_action ('ws_plugin__s2member_during_collective_mods',
$user_id, // The user's ID in WordPress?
array(), // For debugging only. Use empty array here.
'dynamic-user-role-change', // Role change flag.
'modification', // A modification flag.
$new_role, // New role.
$user, // New user object instance.
$old_user // Old user object instance.
);
return;
}
else {
return;
}
}
else {
return;
}
}
}
add_action( 'xprofile_updated_profile', 'crowd_change_role_from_xprofile', 1, 3);