Skip Navigation

[Resolved] Unable to change user role from CRED

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to change a user role using a Toolset form.

Solution:
Because WordPress allows a user to have multiple roles, this requires custom code to remove the existing role for the user and add the new role.
Check this sample code:

add_action('cred_save_data', 'cred_update_user_role_pending',10,2);
 
function cred_update_user_role_pending($post_id, $form_data) {
 
  if ($form_data['id'] == 3386 ) {
    $user = new WP_User( $post_id );
    $user->remove_role('pendingtechnician');
    $user->add_role('technician');
  }
 
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 3 years, 8 months 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.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+00:00)

This topic contains 9 replies, has 2 voices.

Last updated by marioD-5 3 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1718155

Tell us what you are trying to do?
I'm building a front-end page for admins where I have a view that loops "pendingtechnician" and "pendingpharmacist".
Inside the view I have a user edit form (showing only the submit button) that should change the user role to "technician" and another form to "pharmacist".

Is there any documentation that you are following?
I read and tried all reported issues in these forums, and this is the code that I'm using but the user role never gets changed.

/*** Change user role ***/
add_action('cred_save_data', 'cred_update_user_role_pending',10,2);

function cred_update_user_role_pending($post_id, $form_data) {
require_once(ABSPATH.'wp-admin/includes/user.php');

if ($form_data['id'] == 3162 ) {
// update the user role
wp_update_user ( array ( $post_id, 'role' => 'technician' ));
}
}

#1718223

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello and thank you for contacting the Toolset support.

I am not sure why wp_update_user fails. But I'll suggest using set_role.
https://developer.wordpress.org/reference/classes/wp_user/set_role/
Something like:

$user = new WP_User($post_id);
$user->set_role('technician');

There is also no need for the following line, as all the WP functions are already loaded:

require_once(ABSPATH.'wp-admin/includes/user.php');

I hope this helps. Otherwise, please allow me temporary access to your website and let me check this closely. In this case, please give more details, where is the form, view, custom code, etc.
Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1720555

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello and my apologies for the late reply, but I do not work on Sundays and Mondays.

Please check the following screenshot, I believe that you can get what you need without custom code. Just configure the form to save the new role for the user when submitted and that would be enough. No more need for any custom code, unless you have a more complex use case. If yes, please provide more details so I can better advise you.
hidden link

Let me know if this helps or if you have further questions.

#1720577

Thank you for your reply.

I tried this method but the user role isn't changing.

I'm using this code: [cred_user_form form='approve-pending-technician' user="[wpv-user field='ID']"]
I also tried user='53'(my test user) but nothing I do seems to take effect.

I sent you administrator login in my last reply, if you need it to get a better view. I'm sure this will be a trivial issue but for days I wasn't able to figure it out.

Thank you for your assistance,

#1721561

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

I can see the issue and I have checked all the settings from the form and from Toolset Access. Everything seems ok and it should work. I'll need to perform some tests to narrow down this issue, but I can run these tests on a live site without your consent.

Is it possible to have a staging site where I can perform the following tests:
- Activate PHP debugging to check if any errors are being written.
- Check with a default site and only Toolset plugins.
- Check again without Toolset Access.

If we can't have a staging site, I prefer to take a copy of your website and test it locally or on our online platform.
Let me know what would you prefer?

#1721623

Thanks Jamal,

You can take a copy of the site for your local testing. I already performed a full backup.
Hope it won't take you long to figure out what the issue is.

Thanks,

#1721649

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Thank Mario,

Unfortunately, I was not able to take a complete duplicator copy, probably, because of the server timeout restrictions. Check this screenshot hidden link

I took a copy of the database with Duplicator, but I'll still need a copy of the site files. Can you share a zip file of the whole website? If you can exclude the uploads folder, that would be great.
Your next reply will be private to let you share the download link safely.

#1722695

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Thank you for the files.

Unfortunately, I was not able to find out the cause of the issue, and I was not able to implement a new form in a minimal setup(default+theme+only Toolset plugins).
I am approaching our 2nd Tier for assistance on this issue and I'll get back to you as soon as possible.

#1722809

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Our 2nd Tier has confirmed, that the role set on the form settings will not be saved for the user. And that the role modification needs to be done by custom code.

Locally I created another form and I was able to change the user roles with the following code, please update your form_id accordingly:

add_action('cred_save_data', 'cred_update_user_role_pending',10,2);

function cred_update_user_role_pending($post_id, $form_data) {

  if ($form_data['id'] == 3386 ) {
	$user = new WP_User( $post_id );
	$user->remove_role('pendingtechnician');
	$user->add_role('technician');
  }

}

Let us know if that is working for you.

#1722865

My issue is resolved now. Thank you!

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