Support Forum

User mapping based on WP Roles to Forums not working

KB Kelly Brady
Kelly Brady
Member

Hello – I have a test site set up and I’m using Memberpress, Buddypress & Simplepress w/Buddypress integration.

I have several Memberpress membership levels, and I’d like certain memberships to be granted access to certain Simplepress forums upon signup. I have created memberships named “limited”, “premium” & “cms_faculty”; each will  have a corresponding forum.

I have created custom WP Roles based on these these membership names. (Memberpress provided me with a custom plugin that assigns new members at time of sign up to these WP Roles based on the matching membership mentioned above).

In Simplepress I created forums and user groups for each of these groups, and assigned the specific permissions needed. (see attached screenshots). I then set the user group mapping based on WP Roles.

Simplepress seems to be working fine and recognizing the forum permissions once I get a user into the correct User Group; however Simplepress is not putting them in the correct user group based on their WP Role. 

When I create a new user via Memberpress membership signup, they are correctly assigned to the right custom WP Role. Simplepress seems to be ignoring the “Usergroup memberships based on WP role” that I set up and instead is using the “Default usergroup membership”.

What do I need to do to get Simplepress to use the “Usergroup memberships based on WP role” for new signups?

Thanks,

Kelly

permissions.pngusergroups.pngusergroup-memberships.png

11 Answers

New Answer

KB Kelly Brady
Kelly Brady
Member

FYI – I stumbled across a similar post here in the forums, but I don’t fully understand it:

https://simple-press.com/support-forum/sp5-general-topics/how-to-catch-the-add_role-action/page-2/

It appears the issue was resolved with some implementation of ‘set_user_role’, however I don’t fully understand it.

Here is the plugin code that I am using to automatically assign new Memberpress membership levels to WP Roles; perhaps a set_user_role needs to also happen when this code runs?

function memberpress_user_role($txn) {
$user = get_user_by(‘id’, $txn->user_id);

//user doesn’t exist for some reason, or is an admin – we don’t want to wipe out the admin’s role haha
if($user === false || user_can($txn->user_id, ‘manage_options’))
return;

//Leave the user’s WP Role alone, and just add a bbPress Role to them
if($txn->product_id == 25) { //User purchased membership 25: CMS_faculty
$user->remove_role(‘subscriber’);
$user->add_role(‘cms_facility_role’);
return;

//Leave the user’s WP Role alone, and just add a bbPress Role to them
} elseif($txn->product_id == 23) { //User purchased membership 23: limited
$user->remove_role(‘subscriber’);
$user->add_role(‘limited_role’);
return;

//Leave the user’s WP Role alone, and just add a bbPress Role to them
} elseif($txn->product_id == 24) { //User purchased membership 24: premium
$user->remove_role(‘subscriber’);
$user->add_role(‘premium_role’);
return;

}
}
add_action(‘mepr-txn-status-complete’, ‘memberpress_user_role’);

MP Mr Papa
Mr Papa
Member

I am not sure how this memberpress works…  when this plugin assigns a new user the wp role, does it properly use the wp api for this so that appropriate hooks are fired?  if it just manipulates it via the db, then no way for us to know that the wp user got the role..

the code in your second post actually does use the wp api to set the role (ie the $user->add_role function call)… so when that is done, wp notifies plugins such as simple press that the user got the role, so we can act appropriately…

KB Kelly Brady
Kelly Brady
Member

Hi – thanks for the response!

I don’t know if Memberpress properly uses the WP API; I will ask them directly.

The code I posted was provided by them, with the extra IF’s added by me.  The code works – that is, WordPress knows that the person is now in whatever new custom WP Role they’ve been assigned to.

Previously, you discussed this issue with another user in the post below, and you suggested that “set_user_role” was necessary; that add_role by itself was insufficient.

https://simple-press.com/support-forum/sp5-general-topics/how-to-catch-the-add_role-action/page-2/

It would seem to me that if the user has gotten the custom WP Role, wouldn’t Simplepress recognize the user is a member of that group?  That is what does not seem to be happening.

Kelly

MP Mr Papa
Mr Papa
Member

ah, several years ago…  and it was a deficiency in wp… and looks like one they have corrected… though they use a different hook for it too…

so for now, just try changing

$user->add_role

to

$user->set_role

and it should work…

Like the wp did make an update, but looks like we need to check for the new add_user_role hook as well… and I will open a ticket for that…

KB Kelly Brady
Kelly Brady
Member

holy cow – it worked!  Thanks alot!  I’ll let the Memberpress folks know as well in case they get this request again in the future.

Kelly

MP Mr Papa
Mr Papa
Member

thanks for the confirmation…  and as mentioned, we will get the additional wp hook that was added in use as well on our end in the next version…

KB Kelly Brady
Kelly Brady
Member

Mr Papa – This dialog with you and with Memberpress support has prompted them to want to add user role support to the UI of Memberpress.  They have a question for you:

Do they currently use the add_user_role and remove_user_role action hooks from WordPress also, or do they only look at the set_user_role hook?

I think I know the answer to this but wanted to hear it from you if possible.  Thanks!

KB Kelly Brady
Kelly Brady
Member

Memberpress also has a suggestion, which you may have already thought of, but for the sake of thoroughness let me post it here

Paul replied
May 17, 11:48am
It would be great if they could check for add_user_role AND remove_user_role. That way we could set up our addon to both add/remove roles from user’s as they become active/inactive on membership levels.

It can notify plugins if they setup a proper callback for the hooks:

Add A Role Hook: https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-user.php#L524
Remove A Role Hook: https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-user.php#L551
Set User’s Role Hook: https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-user.php#L594 (It sounds like they’re only using this one currently)

MP Mr Papa
Mr Papa
Member

Depends….. How about that?  🙂

Actually depends on whether you want or need users to have more than one role. 

In general, I think single role makes more sense… So I would use the set role. .

KB Kelly Brady
Kelly Brady
Member

Well, in my case I want to map users to different forums based on their role, and they could possibly have more than one role.  This is similar to the issue of hierarchical vs multiple membership “levels” for a Membership plugin.  It really depends on what the business case is, and whether a single level, perhaps in a hierarchy works, or whether the user might be entitled to several of a set of possible services/pages/posts/forums.

Thanks again for your help on this!

Kelly

MP Mr Papa
Mr Papa
Member

certainly possible… users can have more than one wp role… and users can belong to more than one usergroup in simple press…so depends how you implement the gain role stuff…

you can give a user one set of permissions on one forum and another set of permissions on another forum… you can do this via usergroup memberships as well… when you add role to user, they will gain the usergroup membership you have mapped to that role…

just dont have the single usegroup membership option enabled…