We run a membership through our Wordpress/Woocommerce system. We’ve been using Discourse on a subdomain with the SSO for a couple years and it’s been working great. I have code in my functions file that adds/removes Group access when the person joins/cancels respectively.
We’ve added a feature in our membership where people can join a small group of 6-8 people for weekly Zoom calls.
To manage this, we have created a Wordpress Post Type called “Cohorts” that has the weekly call information in it and then assigned those Cohorts to a User using custom user fields.
Here’s what I would like to do:
Create a Group for each of the Cohorts
Create a Subcategory for each of the Cohorts that only the Group from #1 has access to.
Put each of the Members in their respective Group which will give them access to their, and on their, subcategory for their Cohort.
I know how to do #3, but is there a way to programmatically add Groups and Subcategories (with the security settings)?
I can’t find this functionality in the WP-Discourse utilities. Also, looking at the API, I can see where to add categories, but not the security functions.
Yes, it’s possible. You can make any API request to Discourse from Wordpress using the discourse_request utility method, i.e.
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;
$args = array(
"body" => "",
"type" => "post"
);
$response = DiscourseUtilities::discourse_request( $path, $args );
This will format the request correctly and use the API key and user you’ve provided in the WP Discourse settings. You’ll need a global key for the actions you’re referring to (if you’re also using the same key for normal wp discourse functionality).
Note that the “securtiy settings” are handled via the permissions param. You just need to give it an object with group names as the keys and permission level integers as the values, e.g.
Sorry Tim, I’ve been away the past few days. It looks like the discourse_request method needs tweaking to support this. I’ll make a PR for that next week and it’ll probably take another week for that to be merged.
If you want it sooner than that you can use the underlying WP methods like so:
$api_credentials = DiscourseUtilities::get_api_credentials();
if ( is_wp_error( $api_credentials ) ) {
return $api_credentials;
}
$headers = array(
'Api-Key' => sanitize_key( $api_credentials['api_key'] ),
'Api-Username' => sanitize_text_field( $api_credentials['api_username'] ),
'Accept' => 'application/json',
);
$body = array( /* specific to the endpoint you're using */ );
$url = /* The absolute url for the endpoint you're using */;
$opts = array(
'headers' => $headers,
'body' => json_encode( $body ),
'method' => 'PUT'
);
$result = wp_remote_request( $url, $opts );
Hey @timgrahl would you be able to share working function for wordpress with woocommerce for adding / removing users to groups?
I’m using woocommerce with memberships and trying to get this working… if wordpress user has active woocommerce membership then user will be added to X group… when membership is cancelled or expired then user is removed from X group