Skip Navigation

[Resolved] Cred user form role change notification

This support ticket is created 7 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 4 replies, has 2 voices.

Last updated by arnoldd 7 years ago.

Assisted by: Luis Sacristán.

Author
Posts
#514029

Hello,

I have a CRED user form. And I have created a new user role with ACCESS: "onbeslist" and "onbeslist" has the same rights as a visitor. After approval by me it is asignd to a different user role: "customer". This user role can now login to the Webshop on the website.

So far everything is okay.

When I change the user role "onbeslist" to "customer" I do send a mail to this user role automatically:

function user_role_update( $user_id, $new_role ) {
    if ($new_role == 'customer') {
        $site_url = get_bloginfo('wpurl');
        $user_info = get_userdata( $user_id );
        $to = $user_info->user_email;
        $subject = "U bent geaccepteerd op: <em><u>hidden link</u></em>";
        $message = "Hallo " .$user_info->display_name . " uw account op <em><u>hidden link</u></em> is goedgekeurd, u kunt nu inloggen en onze producten bestellen. " ;
        wp_mail($to, $subject, $message);
    }
}
add_action( 'set_user_role', 'user_role_update', 10, 2);

Also this is going well.

However, when submitting the CRED user form creating a "onbeslist" also this same mail is being sent? This is of course not the intention. This only happens whit the ACCESS created user roles.

When I get to the settings of this CRED form and select a custom WP user role (not created by ACCESS) such as administrator then this wil not happen.

Also every time I create a new user role in ACCESS, the permissions in ACCESS control user forms and post form reset. Only the one created by ACCESS. Quite irritating.

#514331

Luis Sacristán
Supporter

Languages: English (English ) Spanish (Español )

Hi @arnoldd

Thanks for consulting Toolset Support
I will be happy to help you with your ticket.

It seems to be a little bug in CRED. When the form is submitted the user is created and then the role is assigned. When WordPress creates an user without role set, it will assign the default role, usually Subscriber.

So, CRED first creates an user with default role and then assign the role, each time ‘set_user_role’ action is called.

My question is why the email is sent when you check if the role is ‘customer’. What role do you have as your default role? You can check in the WP Dashboard > Settings > General > New User Default Role.

To fix your problem, you should add a third parameter to your action callback: $old_roles. It is an array containing the previous roles. With this new parameter, you should check if ‘onbeslist’ is in $old_roles.

For further information please check this doc:
https://developer.wordpress.org/reference/hooks/set_user_role/

About the permission resetting, you are right, it is a bug. I am going to inform our dev team in order to fix it.

Regards
Luis

#514361

Hi Luis,

Thanks for the reply.

"My question is why the email is sent when you check if the role is ‘customer’." No, the mail is sent when i aprove the user. "onbeslist" have restrictions. If the admin changes the role after approval (based on the fields in the user form), the admin sets the rol to "customer". The customer gets the mail that he now is approved and can now log in to place orders.

I have now set the custom rol to: "onbeslist" and the problem seems to be resolved.

So i don't need the "callback: $old_roles", right?

#514427

Luis Sacristán
Supporter

Languages: English (English ) Spanish (Español )

Hi

What I meant is that you checked in your code if the role was a customer:

if ($new_role == 'customer') {

If you have changed the role to ‘onbeslist’ and it works, you should add $old_roles. But be careful, every time and admin changes the role to ‘onbeslist’ the user will receive the same email. I suppose it doesn’t have to happen, but you should take into consideration.

Our dev team is working on the bugs you sent us

Regards
Luis

#516571

Thanks!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.