Manage group membership in Discourse with WP Discourse SSO

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

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.

「いいね!」 3

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.

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

「いいね!」 8

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

「いいね!」 1

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

「いいね!」 3

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).

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

「いいね!」 1

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

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 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:

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;
}
「いいね!」 7

これはサンプルコードであることは承知していますが、かなり重大なバグがあるようです。

支払いが完了しておらず、メンバーシップのステータスが「トークン」の場合

グループは依然としてディスコースユーザーに追加されます :open_mouth:

後で確認しますが、言及する価値があると思います。


「いいね!」 1

このコードを使用して、Discourse コミュニティを Paid Memberships Pro を使用して有料メンバーのみに制限したいと考えています。無料メンバーシップレベルが 1 つと、有料メンバーシップレベルが 2 つあります。

このコードを次のように変更するにはどうすればよいですか。Manage group membership in Discourse with WP Discourse SSO

有料メンバーのみがコミュニティにアクセスできるようにします。それ以外の場合は、有料メンバーシップサインアップページにリダイレクトされます。

ありがとうございます。

参考までに、変更したいコードは次のとおりです。

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 ( /* 何らかの条件 */ ) {
	    wp_safe_redirect( home_url() );

	    exit;
    }
}

/* 何らかの条件 */ コメントを、有料会員資格を持っていないユーザーに対して true を返す条件に置き換える必要があります。これらのユーザーは、会員登録ページにリダイレクトされます。

現在、テストサイトに Paid Memberships Pro プラグインをインストールしていませんが、ドキュメントによると、pmpro_hasMembershipLevel 関数を使用して、ユーザーが特定の会員資格レベルを持っているかどうかを確認できるようです。https://www.paidmembershipspro.com/documentation/content-controls/require-membership-function/。

pmpro_hasMembershipLevel 関数を使用するには、2つの有料会員レベルのID(または名前)を知る必要があります。これらのIDは、pmpの「会員レベル」管理ページから取得できます。たとえば、2つの有料レベルのIDが1と2の場合、次の条件を使用できます。

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

コードに代入すると、次のようになります。

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;
    }
}

変更する必要があるもう1つの行は次のとおりです。

wp_safe_redirect( home_url() );

これは現在、ユーザーをサイトのホームページにリダイレクトするように設定されています。サイトのサインアップページのパスまたは完全なURLにリダイレクトするように変更する必要があります。

wp_safe_redirect( /* path_to_your_signup_page */ );

このコードはテストしていません。ライブサイトの functions.php ファイルに直接変更を加える場合は、コードに間違いやタイプミスがあった場合に備えて、サイトのバックエンドからサイトの functions.php ファイルにアクセスできることを確認してください。

「いいね!」 4

WordPress Discourse の Create or Sync Discourse Users on Login オプションが有効になっている場合、WordPress サイトのすべてのユーザーに対して Discourse アカウントが自動的に作成されます。これは、OP で概説されている方法で Discourse へのログインがブロックされている場合でも同様です。適切なメンバーシップレベルを持っていないユーザーに対してアカウントが自動的に作成されるのを防ぐ方法についての詳細は、この投稿の後半にあります: How to prevent some WP users from being able to login to Discourse - #2 by simon.

要するに:

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;
}

OP にそれらの詳細を更新するよう自分自身にリマインダーを設定しています。

「いいね!」 2

サイモンさん、ありがとうございます。あなたの指示通りにウェブサイトに設定できました。

「いいね!」 1

確認です。

レベルなし(ユーザーなし):0
無料レベル:1
有料レベルは2つあります:2、3

あなたはこう言いました:

あなたがする必要があるのは、コードの /* 何らかの条件 */ コメントを、有料会員資格を持たないユーザーに対して true を返す条件に置き換えることです。これらのユーザーは、会員登録ページにリダイレクトできます。

そして

pmpro_hasMembershipLevel 関数を使用するには、2つの有料会員レベルのID(または名前)を知る必要があります。これらのIDは、pmpの「会員レベル」管理ページから取得できます。たとえば、2つの有料レベルのIDが1と2の場合、次の条件を使用できます。

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

これらの記述は矛盾していますか?

私の理解では:
有料会員ではないユーザーを会員サインアップページにリダイレクトするには、次を使用する必要があります。

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

もし私が間違っていたら訂正してください。

よろしくお願いします。

条件が true を返した場合、ユーザーはサインアップページにリダイレクトされます。

あなたのケースで最も安全なのは、ユーザーがレベル2または3のいずれも持っていない場合に true を返す条件を使用することだと思います:

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

「いいね!」 1

ありがとうございます。2番目のスニペットで「!」を付け忘れていました。

「いいね!」 1