Support Forum

How to catch the add_role action

SA Samantha
Samantha
Member

Hi –

I’m using MemberPress with my site, and I have multiple course products that a user can purchase.  I am running into trouble because SimplePress is not picking up the “add_role” action that we are using to add multiple roles for a user.  I’ve read the other post that recently had a similar issue, and I used the Event Logger to confirm that SimplePress doesn’t appear to catch the “add_role” action.

Is it possible to address this?  Otherwise, my students are not going to have access to their course forums.

Thanks!
Samantha

17 Answers

New Answer

MP Mr Papa
Mr Papa
Member

not sure what you mean by the ‘add_role’ action…  wp does not have such an action…

when wp changes the role of a user, it fires the ‘set_user_role’ action…  This is to let themes and plugins know that a user’s role has changed…  if a role is being changed in a non standard manner, then plugins wont know about it..

if you are changing the role independently of the wp api, you could fire the same action and things should work…  the action should be fired (as wp does) with three arguments… the first is the user id (integer), the second is the new role (string) and the third argument is the old roles (array)…

of course, its possible there is an implementation of wp role changing that has changed in wp or a path we are not aware of, but the role changing seems to be working for  users…

SA Samantha
Samantha
Member

Hi –

Thanks for the reply.  Is there an add_user_role action?

I’ve got a message in to the MemberPress team to see if there’s anything they can do on their end to help resolve this.  I’ll post again if I have something concrete to ask.

Thx!
Samantha

YS Yellow Swordfish
Yellow Swordfish
Member

I can find no mention of an ‘add_user_role’ action hook in the WordPress core code. We have, sadly, come across a few plugins over the years that replace a part of core functionality but do not implement the standard hooks. Actually, I believe we were guilty of this on at least one occasion many years ago!

Please do let us know what the membership people come back with. If they can provide a custom hook that IS available that passes the same data then we can probably help you implement that

MP Mr Papa
Mr Papa
Member

I only know of the action I mentioned previously…  where are you getting these actions from?  docs?  or hooks that your membership plugin is firing?

SA Samantha
Samantha
Member

Hi –

The MemberPress guys came back and said the add_role is a supported action/problem-with-post-edit-buttonook in the WordPress codex.  Here’s a link:

 

http://codex.wordpress.org/Class_Reference/WP_User#add_role.28_.24role_.29

 

Is there any way to get support for this action into SimplePress?  It would allow my new users to once again have their Forum access set automatically, instead of me having to go in and update it for each new user.

 

Thanks!

Samantha

SA Samantha
Samantha
Member

In case you prefer the Code Reference, I also found it there:

https://developer.wordpress.org/reference/classes/wp_user/add_role/

 

Thx!

YS Yellow Swordfish
Yellow Swordfish
Member

I will look into this tonight… and come back to you.

YS Yellow Swordfish
Yellow Swordfish
Member

‘add_role’ is a core function NOT an action hook. No plugins can extract any information from a function such as this.

I was also under the impression that it was your users that we needed to know about which is not the same thing as a role.

As Mr Papa said above – the standard WordPress action hook for adding a user to a role is the ‘set_user_role’ action…

SA Samantha
Samantha
Member

Hi again,

I’m setting Forum permissions based on user roles, and some users may have multiple roles.  For existing users, MemberPress is using “add_role” to add the additional roles for each user.  

They are telling me the exact opposite of what you’re saying – they tell me that lots of plug-ins catch the add_role and are able to use that to set up additional info.

I’ll go back to them again with your comments above, and see what they say.

Thanks!

YS Yellow Swordfish
Yellow Swordfish
Member

Yes please do come back. This clearly needs to be sorted out.

MP Mr Papa
Mr Papa
Member

interesting… it does seem possible to ADD a wp role to a user via the add_role function… however, it may or may not have any impact on the user capabilities, since a level check is done…

additionally, the user will now have multiple wp roles which could be construed as problematic since multiple wp roles may map to multiple sp usergroups… from a wp perspective you will just get the higher level of the two roles… but still have both levels…

there is a corresponding remove role function which can remove a specific level…

however, neither the add role function or the remove role function have applicable hooks fired by wp that lets other plugins know that roles were changing (which is probably why core uses set role)…  the set role replaces the current role with the new role such that the user maintains single wp role…

there is a level change hook that might could be tied into, but its potentially problematic in that a single level can belong to multiple roles… so from the level hook, I am not sure we can know which role was added (or removed)…

so, will have to do more research, but at this point, adding a role, vice setting the role, may not tell plugins about the role change…  and a user may end up with two roles which would cause an issue when mapping roles to user groups…

SA Samantha
Samantha
Member

Hi –

I am reopening this thread because the fix I thought I had is not actually working.

I was able to get the MemberPress guys to modify the plug-in they provided so that it would call the set_user_role action, per your previous replies.  However, when I added a course to an existing site and forum user, the user’s Forum roles were not updated to reflect the new permissions.  I can see in the Event Logger that the set_user_role was triggered (see attached screenshot), but the user’s permissions were not updated (see second screenshot).

I am opening a new course for registration, and would really like to understand what is happening here, so I don’t have to manually add permissions for every student.

Thanks!
SamanthaScreen-Shot-2015-01-12-at-2.40.07-PM.pngScreen-Shot-2015-01-12-at-2.40.28-PM.png

SA Samantha
Samantha
Member

I don’t know if this is relevant or not, since I can see the set_user_role action in the SP event logger, but this is how the set roles action is being called:

do_action(‘set_user_role’, $user->ID, ‘participant_ur_jan2015’, $old_roles);

//Doesn’t actually change user’s role — just fires this hook for Simple Press’ sake

 

Thx,

Samantha

YS Yellow Swordfish
Yellow Swordfish
Member

Before looking any deeper I have to ask the obvious first question just to be sure. Did you create the new user group and map the new user group to this WordPress role before updating this particular user?