WP Discourse functions
The WP Discourse plugin has a couple of functions that can be used to associate WordPress users with Discourse groups.
These functions are only enabled on sites that are using WordPress as the SSO provider for Discourse.
add_user_to_discourse_group( $user_id, $group_names )
View Coderemove_user_from_discourse_group( $user_id, $group_names )
View Code
Each of these functions take the same two parameters:
$user_id
: the WordPress user’s id$group_names
: a comma separated list of Discourse group names (with no spaces between names.)
The functions have been made with the assumptions that every membership plugin will do an action
when it adds or removes WordPress users from a level, and that the parameters sent with these actions will include the WordPress user’s ID and the membership level id. I have looked at a few WordPress membership plugins, and they function this way.
Using the functions with a membership plugin
-
Look through the hooks and filters documentation of whatever membership plugin you are using. Look for actions that correspond to
add_membership_level
andremove_membership_level
. -
At the top of your file (for example
functions.php
create an alias for theWPDiscourse
namespace. This will make it easier to use the functions in your code:
use WPDiscourse\Utilities\Utilities as DiscourseUtilities
-
Write a couple of functions to add and remove members from Discourse groups.
-
Hook into the functions after checking the the WP Discourse plugin is loaded.
An example using the PaidMembershipsPro plugin
I’m using this plugin for an example because I’ve been asked about it, it has a free version, and it’s easy to setup. The documentation for its hooks and filters is here. This example is incomplete. It just ties into two hooks, one that is called when a membership is created, and another that is called when a membership is cancelled. There are other actions that are called when membership levels are changed, or when all membership levels are cancelled at the same time. You’ll need to look through the docs…
<?php
// functions.php file
// Allows you to call the wp-discourse functions without having to write the namespace each time you use them.
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;
// Returns the Discourse group name that is associated with a membership id. With
// PaidMembershipsPro, you can see the membership id on the Membership Levels page. With other
// membership plugins, you may need to call a function to get access to a `membership_id/membership_name` array.
function dcpmp_get_level_for_id( $id ) {
$levels_to_discourse_groups = array(
1 => 'walking',
2 => 'running'
);
if ( empty( $levels_to_discourse_groups[ $id ] ) ) {
return new WP_Error( 'pmpdc_group_not_set_error', 'A Discourse group has not been assigned to the level.' );
}
return $levels_to_discourse_groups[ $id ];
}
// Adds a user to a Discourse group if there is a group associated with the membership level.
function dcpmp_add_member_to_group( $member_order ) {
if ( ! empty( $member_order->membership_id ) && ! empty( $member_order->user_id ) ) {
$user_id = $member_order->user_id;
$group_name = dcpmp_get_level_for_id( $member_order->membership_id );
if ( is_wp_error( $group_name ) ) {
return null;
}
// Adds the user to the Discourse group.
$result = DiscourseUtilities::add_user_to_discourse_group( $user_id, $group_name );
if ( ! empty( $result->success ) ) {
// If the user has been added to the group, add a metadata key/value pair that can be used later.
add_user_meta( $user_id, "dcpmp_group_{$group_name}", 1, true );
}
return $result;
}
return new WP_Error( 'dcpmp_order_not_set_error', 'There was an error with the PMP order.' );
}
// Removes a user from a Discourse group.
function dcpmp_remove_member_from_group( $level_id, $user_id, $cancel_level ) {
if ( ! empty( $cancel_level ) ) {
$group_name = dcpmp_get_level_for_id( $cancel_level );
if ( is_wp_error( $group_name ) ) {
return null;
}
// Removes the user.
$result = DiscourseUtilities::remove_user_from_discourse_group( $user_id, $group_name );
if ( ! empty( $result->success ) ) {
// Remove the membership level metadata key.
delete_user_meta( $user_id, "dcpmp_group_{$group_name}" );
}
return $result;
}
return null;
}
// Hook into the PaidMembershipsPro actions.
// Make sure to check that the Discourse class exists. If not, and you deactivate wp-discourse, this will crash your site.
if ( class_exists( '\WPDiscourse\Discourse\Discourse' ) ) {
add_action( 'pmpro_added_order', 'dcpmp_add_member_to_group' );
add_action( 'pmpro_after_change_membership_level', 'dcpmp_remove_member_from_group', 10, 3 );
}
Restricting access to Discourse when a membership doesn’t exist
There is a wpdc_sso_provider_before_sso_redirect
action hook that can be used to restrict access to your forum to only WordPress users who have a membership. In the SSO login process the action occurs after the user has logged into WordPress, but before they are redirected to Discourse with their login credentials.
You will need to setup your own condition to check for the existence of a membership. Make sure you call exit
after the redirect.
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;
}
}