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' ));
}
}
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. **
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.
lien caché
Let me know if this helps or if you have further questions.
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,
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?
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,
Thank Mario,
Unfortunately, I was not able to take a complete duplicator copy, probably, because of the server timeout restrictions. Check this screenshot lien caché
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.
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.
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.
My issue is resolved now. Thank you!