Using Custom Meta Data and SSO to auto-add groups

@simon, a few years back you helped us add code to auto-add groups to users based on their membership in WordPress/MemberPress. That works great. We are now hoping to auto add/remove groups based on user meta data. I have the below, which feels like it should work with the other code we have:

$nbes_session = get_user_meta( $current_user->ID, 'mepr_national_board_certifying_exam_session' , true );
if (! empty( $nbes_session )) {
  $groups_to_add[] = 'nbes_' + $nbes_session;
} else {
  $groups_to_remove[] = 'nbes_' + $nbes_session;
} 

It doesn’t seem to be adding that group to the user though. Is there something I am missing? Thanks in advance for your help.

Yes, I don’t think this is working as you expect it to:

$groups_to_add[] = 'nbes_' + $nbes_session;

The concatenation operator in PHP is ., not +. Try changing the code to this:

$nbes_session = get_user_meta( $current_user->ID, 'mepr_national_board_certifying_exam_session' , true );
if (! empty( $nbes_session )) {
  $groups_to_add[] = 'nbes_' . $nbes_session;
} else {
  $groups_to_remove[] = 'nbes_' . $nbes_session;
}
2 Likes

@simon, ah, brain fart! Thank you for your help! That fixed the issue.

2 Likes