The WP Discourse plugin has a couple of functions that can be used to associate WordPress users with Discourse groups. These functions could be useful if you have a membership plugin installed on your WordPress site. They are geared towards sites that are using WordPress as the SSO provider for Discourse, but could possibly be used without SSO.
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.
The two WP Discourse functions are:
add_user_to_discourse_group( $user_id, $group_name, $force_update = false )
remove_user_from_discourse_group( $user_id, $group_name, $force_update = false )
add user to discourse group
add_user_to_discourse_group
takes three parameters: the WordPress user’s id, the Discourse group name, and $force_update
. $force_update
refreshes the data that is stored on WordPress about Discourse groups, it defaults to false, and you can ignore it unless you have added a group to Discourse within 10 minutes of running the function.
The function first checks if the user exists on Discourse, if they don’t, it creates the user through the API. They are then added to the Discourse group.
View Code
remove user from discourse group
remove_user_from_discourse_group
takes three parameters: the WordPress user’s id, the Discourse group name, and $force_update
. It removes the user from the named group on Discourse.
View Code
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
and remove_membership_level
.
-
At the top of your file (for example functions.php
create an alias for the WPDiscourse
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 );
}