# 使用自定义元数据和 SSO 自动添加组

**URL:** https://meta.discourse.org/t/using-custom-meta-data-and-sso-to-auto-add-groups/169271
**Category:** WordPress
**Created:** [2020年十一月4日 19:20 UTC](https://meta.discourse.org/t/using-custom-meta-data-and-sso-to-auto-add-groups/169271 "2020-11-04T19:20:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Andrew\_Pautler](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/andrew_pautler/32/120959_2.png) [@Andrew\_Pautler](https://meta.discourse.org/u/Andrew_Pautler)
#### Post date: [2020年十一月4日 19:20 UTC](https://meta.discourse.org/t/using-custom-meta-data-and-sso-to-auto-add-groups/169271/1 "2020-11-04T19:20:50Z")

</div>

@simon，几年前您曾帮我们在代码中添加了根据用户在 WordPress/MemberPress 中的会员身份自动添加用户组的功能，效果非常好。现在我们希望根据用户元数据自动添加或移除用户组。我有以下代码，感觉应该能与我们现有的其他代码配合工作：

```php
$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;
}

```

但这似乎并没有将相应的组添加到用户中。我是否遗漏了什么？提前感谢您的帮助。

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2020年十一月4日 19:39 UTC](https://meta.discourse.org/t/using-custom-meta-data-and-sso-to-auto-add-groups/169271/2 "2020-11-04T19:39:56Z")

</div>

> [@Andrew\_Pautler](#):
>
> 我是不是漏掉了什么？

是的，我认为这并不像您预期的那样工作：

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

```

PHP 中的连接运算符是 `.`，而不是 `+`。请将代码修改为：

```php
$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;
}

```

---

<div class="post-metadata">

### Author: ![Andrew\_Pautler](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/andrew_pautler/32/120959_2.png) [@Andrew\_Pautler](https://meta.discourse.org/u/Andrew_Pautler)
#### Post date: [2020年十一月4日 19:45 UTC](https://meta.discourse.org/t/using-custom-meta-data-and-sso-to-auto-add-groups/169271/3 "2020-11-04T19:45:23Z")

</div>

@simon，啊，脑子短路了！谢谢你的帮助！问题解决了。
