# إدارة عضوية المجموعة في Discourse باستخدام WP Discourse SSO

**URL:** https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724
**Category:** Administrators
**Tags:** groups, wordpress, payments, how-to
**Created:** [24 نوفمبر 2017، 6:55ص UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724 "2017-11-24T06:55:17Z")
**Posts on this page:** 18
**Page:** 4

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [12 أبريل 2019، 11:03م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/72 "2019-04-12T23:03:01Z")

</div>

> [@simon](#):
>
> I can only reproduce it if a username is changed on Discourse after an account is created on WordPress

Holy 🐄! How’d you manage to find it!? Nice work.

---

<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: [12 أبريل 2019، 11:06م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/73 "2019-04-12T23:06:06Z")

</div>

> [@pfaffman](#):
>
> How’d you manage to find it!?

By adding log statments to my development site. This should be an easy fix, but I’d rather not push the change to WordPress before I test it out some more.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [12 أبريل 2019، 11:10م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/74 "2019-04-12T23:10:21Z")

</div>

Sure you have a reasonable development environment. Like a real developer. That I understand (in spite of my not having such!) But even thinking to try usernames that had changed… I guess the 2 was the clue.

---

<div class="post-metadata">

### Author: ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)
#### Post date: [15 مايو 2019، 2:16م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/75 "2019-05-15T14:16:36Z")

</div>

Here’s a slightly different method to handle Paid Memberships Pro group sync. Pop this into your `functions.php` file. Expected behavior is add upon subscribe and remove upon cancellation. Thanks to @dfriestedt for sponsoring.

[https://github.com/justindirose/dc-pmp-group-sync](https://github.com/justindirose/dc-pmp-group-sync)

---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [15 مايو 2019، 2:55م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/76 "2019-05-15T14:55:57Z")

</div>

Awesome, anything stopping this being wrapped into a plugin for customers who aren’t confident editing PHP?

---

<div class="post-metadata">

### Author: ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)
#### Post date: [15 مايو 2019، 3:01م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/77 "2019-05-15T15:01:58Z")

</div>

No, probably not, but PHP/WP development isn’t my forte, and I don’t plan on personally actively maintaining this code long-term.

---

<div class="post-metadata">

### Author: ![dylanb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dylanb/32/139102_2.png) [@dylanb](https://meta.discourse.org/u/dylanb)
#### Post date: [31 مايو 2019، 7:02م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/78 "2019-05-31T19:02:46Z")

</div>

I am implementing the PMPro code above and am hung up on something. My understanding is that this code only fires when users buy subscription or cancel a subscription.

Is there a way that I can auto-sync users as they are created on Discourse? In my case, I have some users who are subscribers but don’t have a Discourse accounts (will be created via SSO).

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [31 مايو 2019، 8:10م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/79 "2019-05-31T20:10:56Z")

</div>

If wordpress is the SSO master then you can have it update groups at every login.

---

<div class="post-metadata">

### Author: ![dylanb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dylanb/32/139102_2.png) [@dylanb](https://meta.discourse.org/u/dylanb)
#### Post date: [31 مايو 2019، 8:20م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/80 "2019-05-31T20:20:20Z")

</div>

Is there a code snippet that shows an example of updating groups on WP login?

---

<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: [31 مايو 2019، 10:08م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/81 "2019-05-31T22:08:50Z")

</div>

> [@dylanb](#):
>
> My understanding is that this code only fires when users buy subscription or cancel a subscription.

The `add_user_to_discourse_groups` function will create a Discourse user if it doesn’t already exist. If that function is being called for your users, then you should be fine. Where you might run into problems will be for existing WordPress users who got their membership before the code was added to your site. If that is the case, you will probably need to add the users to Discourse groups through the SSO parameters that are passed to Discourse on login.

To pass additional SSO parameters with the [WP Discourse](https://github.com/discourse/wp-discourse) plugin, you can hook into the `'wpdc_sso_params'` filter. The parameter you need to use to add users to a Discourse group is called `add_groups`. You can use it with something like this:

```php
add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params', 10, 2 );
function wpdc_custom_sso_params( $params, $user ) {
    if ( /* some condition that returns true if groups should be added for the user */ ) {
        $params['add_groups'] = 'comma,separated,group,names'; // Don't use spaces between names.
    }

    return $params;
}

```

---

<div class="post-metadata">

### Author: ![Fma965](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fma965/32/259795_2.png) [@Fma965](https://meta.discourse.org/u/Fma965)
#### Post date: [18 يوليو 2022، 8:57م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/95 "2022-07-18T20:57:21Z")

</div>

أدرك أن هذه مجرد عينة من التعليمات البرمجية ولكن يبدو أن بها خطأً فادحًا.

إذا لم تكتمل عملية الدفع وكان وضع العضوية “token”

 ![image](https://global.discourse-cdn.com/meta/original/4X/7/3/7/7379462f653286b5c7e6734763d7d0b4c5c927de.png)

فلا يزال يتم إضافة المجموعة إلى مستخدم Discourse 😮

سأبحث في هذا الأمر لاحقًا ولكني أعتقد أنه يستحق الذكر

 ![image](https://global.discourse-cdn.com/meta/original/4X/1/5/a/15a1a5205d463a6089c81f52ee251999ad276ff9.png)  
 ![image](https://global.discourse-cdn.com/meta/original/4X/f/8/4/f848f56ac4d217c4ef4c6f1e24cdb7c850b5d9cb.png)

---

<div class="post-metadata">

### Author: ![HonestOpinions](https://avatars.discourse-cdn.com/v4/letter/h/7c8e57/32.png) [@HonestOpinions](https://meta.discourse.org/u/HonestOpinions)
#### Post date: [23 أبريل 2023، 9:13م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/96 "2023-04-23T21:13:57Z")

</div>

أرغب في استخدام هذا الرمز لتقييد مجتمع Discourse الخاص بي حصريًا للأعضاء المدفوعين باستخدام Paid Memberships Pro. لدي مستوى عضوية مجاني ومستويان للعضوية المدفوعة.

كيف يمكنني تعديل هذا الرمز: [Manage group membership in Discourse with WP Discourse SSO](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724#restricting-access-to-discourse-when-a-membership-doesnt-exist-4)  
بحيث يتمكن الأعضاء المدفوعون فقط من الوصول إلى المجتمع. وإلا، سيتم إعادة توجيههم إلى صفحة التسجيل للعضويات المدفوعة.

شكرا لك.

---

<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: [23 أبريل 2023، 10:52م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/97 "2023-04-23T22:52:08Z")

</div>

> [@HonestOpinions](#):
>
> كيف يمكنني تعديل هذا الكود: [إدارة عضوية المجموعة في Discourse باستخدام WP Discourse SSO](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724#restricting-access-to-discourse-when-a-membership-doesnt-exist-4)
> 
> بحيث يتمكن فقط الأعضاء الذين يدفعون من الوصول إلى المجتمع. وإلا، سيتم إعادة توجيههم إلى صفحة الاشتراك في العضويات المدفوعة.

للرجوع إليها، إليك الكود الذي تريد تعديله:

```php
add_action( 'wpdc_sso_provider_before_sso_redirect', 'wpdc_custom_check_user_membership', 10, 2 );
function wpdc_custom_check_user_membership( $user_id, $user ) {
    if ( /* some condition */ ) {
	    wp_safe_redirect( home_url() );

	    exit;
    }
}

```

ما تحتاج إلى القيام به هو استبدال التعليق `/* some condition */` في الكود بشرط يعيد `true` للمستخدمين الذين ليس لديهم عضويات مدفوعة. يمكن بعد ذلك إعادة توجيه هؤلاء المستخدمين إلى صفحة الاشتراك في عضويتك.

لا يوجد لدي حاليًا إضافة Paid Memberships Pro مثبتة على موقع الاختبار الخاص بي، ولكن من وثائقهم، يبدو أنه يمكنك استخدام دالة `pmpro_hasMembershipLevel` للتحقق مما إذا كان لدى المستخدم مستوى عضوية معين: [Using the pmpro\_hasMembershipLevel() Function](https://www.paidmembershipspro.com/documentation/content-controls/require-membership-function/).

> [@HonestOpinions](#):
>
> لدي مستوى عضوية مجاني ومستويان للعضوية المدفوعة.

لاستخدام دالة `pmpro_hasMembershipLevel`، تحتاج إلى معرفة معرفات (أو أسماء) مستويي العضوية المدفوعة لديك. يمكنك الحصول على هذه المعرفات من صفحة “مستويات العضوية” في لوحة تحكم pmp. على سبيل المثال، إذا كانت معرفات مستوييك المدفوعين هي 1 و 2، يمكنك استخدام الشرط التالي:

`if (! pmpro_hasMembershipLevel(array(1, 2), $user_id))`

عند استبدالها في الكود، سيكون ذلك:

```php
add_action( 'wpdc_sso_provider_before_sso_redirect', 'wpdc_custom_check_user_membership', 10, 2 );
function wpdc_custom_check_user_membership( $user_id, $user ) {
    if (! pmpro_hasMembershipLevel( array( 1, 2 ), $user_id ) ) {
	    wp_safe_redirect( home_url() );

	    exit;
    }
}

```

السطر الآخر الذي ستحتاج إلى تغييره هو:

`wp_safe_redirect( home_url() );`

وهو حاليًا مضبوط لإعادة توجيه المستخدمين إلى الصفحة الرئيسية للموقع. ستحتاج إلى تغييره لإعادة التوجيه إلى المسار أو عنوان URL الكامل لصفحة الاشتراك في موقعك:

`wp_safe_redirect( /* path_to_your_signup_page */ );`

لاحظ أنني لم أختبر هذا الكود. إذا كنت تجري تغييرات مباشرة على ملف `functions.php` في موقعك المباشر، فتأكد من أن لديك إمكانية الوصول إلى ملف `functions.php` الخاص بالموقع من لوحة تحكم الموقع، فقط في حالة وجود أي أخطاء أو أخطاء إملائية في الكود.

---

<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: [23 أبريل 2023، 11:02م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/98 "2023-04-23T23:02:16Z")

</div>

تركت لنفسي ملاحظة هنا تتعلق بـ [https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724#restricting-access-to-discourse-when-a-membership-doesnt-exist-4](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724#restricting-access-to-discourse-when-a-membership-doesnt-exist-4). إذا تم تمكين خيار [WP Discourse](https://github.com/discourse/wp-discourse) **إنشاء أو مزامنة مستخدمي Discourse عند تسجيل الدخول** ، فسيتم إنشاء حسابات Discourse تلقائيًا لجميع المستخدمين في موقع WordPress، حتى لو تم منعهم من تسجيل الدخول إلى Discourse عبر الطريقة الموضحة في المنشور الأصلي. التفاصيل حول كيفية منع إنشاء الحسابات تلقائيًا للمستخدمين الذين ليس لديهم مستوى العضوية المناسب موجودة في الجزء الثاني من هذا المنشور: [How to prevent some WP users from being able to login to Discourse - #2 by simon](https://meta.discourse.org/t/how-to-prevent-some-wp-users-from-being-able-to-login-to-discourse/93234/2?u=simon).

بشكل أساسي:

```php
add_filter( 'wpdc_bypass_sync_sso', 'wpdc_custom_bypass_sync_sso', 10, 3 );
function wpdc_custom_bypass_sync_sso( $bypass_sync, $user_id, $user ) {
    if ( /* شرط يعيد true إذا كان المستخدم لا يفي بمتطلبات العضوية */ ) {

        $bypass_sync = true;
    }

    return $bypass_sync;
}

```

أقوم بتعيين تذكير لنفسي لتحديث المنشور الأصلي بهذه التفاصيل.

---

<div class="post-metadata">

### Author: ![HonestOpinions](https://avatars.discourse-cdn.com/v4/letter/h/7c8e57/32.png) [@HonestOpinions](https://meta.discourse.org/u/HonestOpinions)
#### Post date: [24 أبريل 2023، 7:54م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/99 "2023-04-24T19:54:58Z")

</div>

شكراً لك يا سيمون. تمكنت من إعداد هذا لموقعي باستخدام تعليماتك.

---

<div class="post-metadata">

### Author: ![HonestOpinions](https://avatars.discourse-cdn.com/v4/letter/h/7c8e57/32.png) [@HonestOpinions](https://meta.discourse.org/u/HonestOpinions)
#### Post date: [7 مايو 2023، 5:32ص UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/100 "2023-05-07T05:32:27Z")

</div>

توضيح.

لا يوجد مستوى (ليس مستخدمًا): 0  
مستوى مجاني = 1  
لدي مستويان مدفوعان: 2، 3

لقد قلت

> ما تحتاج إلى القيام به هو استبدال التعليق `/* some condition */` في الكود بشرط يعيد `true` للمستخدمين الذين ليس لديهم عضويات مدفوعة. يمكن بعد ذلك إعادة توجيه هؤلاء المستخدمين إلى صفحة الاشتراك في عضويتك.

و

> لاستخدام الدالة `pmpro_hasMembershipLevel`، تحتاج إلى معرفة معرفات (أو أسماء) مستويي العضوية المدفوعة لديك. يمكنك الحصول على هذه المعرفات من صفحة “مستويات العضوية” في لوحة تحكم pmp. على سبيل المثال، إذا كانت معرفات مستويي العضوية المدفوعة لديك هي 1 و 2، يمكنك استخدام الشرط التالي:
> 
> `if (! pmpro_hasMembershipLevel(array(1, 2), $user_id))`

هل هذه العبارات متناقضة؟

حسب فهمي:  
يجب أن أستخدم ما يلي:

`if (! pmpro_hasMembershipLevel(array(0,1), $user_id))`

لإعادة توجيه المستخدمين الذين ليس لديهم عضوية مدفوعة إلى صفحة تسجيل الأعضاء.

يرجى تصحيحي إذا كنت مخطئًا.

شكرا.

---

<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: [7 مايو 2023، 3:17م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/101 "2023-05-07T15:17:48Z")

</div>

> [@HonestOpinions](#):
>
> في فهمي:  
> يجب أن أستخدم ما يلي:
> 
> if (! pmpro\_hasMembershipLevel(array(0,1), $user\_id))
> 
> لإعادة توجيه المستخدمين الذين ليس لديهم عضوية مدفوعة إلى صفحة تسجيل الأعضاء.

إذا أعادت الشرط القيمة `true`، فسيتم إعادة توجيه المستخدمين إلى صفحة التسجيل الخاصة بك.

> [@HonestOpinions](#):
>
> لدي مستويان مدفوعان: 2، 3

أعتقد أن أكثر الأشياء أمانًا للقيام به في حالتك هو استخدام شرط يعيد القيمة `true` إذا لم يكن لدى المستخدم أي من المستويين 2 أو 3:

`if (! pmpro_hasMembershipLevel(array(2, 3), $user_id))`

---

<div class="post-metadata">

### Author: ![HonestOpinions](https://avatars.discourse-cdn.com/v4/letter/h/7c8e57/32.png) [@HonestOpinions](https://meta.discourse.org/u/HonestOpinions)
#### Post date: [7 مايو 2023، 5:36م UTC](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724/102 "2023-05-07T17:36:03Z")

</div>

شكراً لك. لقد فاتني علامة “!” في المقتطف الثاني الخاص بك.

[Previous page](https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724.md?page=3)
