Support Forum

Advanced Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
general-topic
User mapping based on WP Roles to Forums not working
Avatar
Kelly Brady
Member
sp_UserOfflineSmall Offline
May 16, 2016 - 6:40 pm

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.pngImage Enlargerusergroups.pngImage Enlargerusergroup-memberships.pngImage Enlarger

Avatar
Kelly Brady
Member
sp_UserOfflineSmall Offline
May 16, 2016 - 7:04 pm

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

https://simple-press.com/suppo.....on/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');

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
May 16, 2016 - 9:02 pm

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

Avatar
Kelly Brady
Member
sp_UserOfflineSmall Offline
May 16, 2016 - 9:11 pm

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/suppo.....on/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

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
May 16, 2016 - 9:22 pm

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

Avatar
Kelly Brady
Member
sp_UserOfflineSmall Offline
May 16, 2016 - 9:31 pm

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

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
May 17, 2016 - 1:32 am

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

Avatar
Kelly Brady
Member
sp_UserOfflineSmall Offline
May 17, 2016 - 1:43 pm

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!

Avatar
Kelly Brady
Member
sp_UserOfflineSmall Offline
May 17, 2016 - 1:56 pm

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/W.....r.php#L524
Remove A Role Hook: https://github.com/WordPress/W.....r.php#L551
Set User's Role Hook: https://github.com/WordPress/W.....r.php#L594 (It sounds like they're only using this one currently)

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
May 17, 2016 - 4:33 pm

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

Forum Timezone: Europe/Stockholm
Most Users Ever Online: 1170
Currently Online:
Guest(s) 1
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Mr Papa: 19448
Ike: 2086
Brandon: 864
kvr28: 804
jim: 650
FidoSysop: 577
Conrad_Farlow: 531
fiddlerman: 358
Stefano Prete: 325
Member Stats:
Guest Posters: 619
Members: 17361
Moderators: 0
Admins: 4
Forum Stats:
Groups: 7
Forums: 17
Topics: 10127
Posts: 79625