Tell us what you are trying to do?
I've created a form to edit non-admin users with a drop down to select a user role (uses a view to populate a generic field). I'm then using a custom function to update the user role when the form is submitted, but I'm getting the error in the attached image. It looks to be a permissions issue, but as you'll see in the screens the form is set for all user roles to be able to access it (for testing).
add_action('cred_save_data', 'yls_update_user_profile',10,2);
function yls_update_user_profile($user_id,$form_data) {
// if a specific form
if ($form_data['id']==1016)
{
$user_role = $_POST['user-role'];
wp_update_user( array( 'ID' => $user_id, 'role' => $user_role ) );
}
}
Is there any documentation that you are following?
What is the link to your site?
I can provide the dev site address and toolset login via secure message.
Hi Matt,
Thank you for contacting us and I'd be happy to assist.
The code snippet that you've shared seems to be correct, but to troubleshoot this, I'll suggest the following steps:
1. Please make sure that WordPress, active theme and plugins are all updated to the latest versions.
2. It would be interesting to test if this is happening only for a specific user, or its the same for a newly created user as well.
3. In case the issue still persists, you're welcome to share temporary admin login details, along with the link to the form page.
Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.
regards,
Waqar
Hi Matt,
Thank you for sharing further details.
During testing on my own website with a similar form and custom code, I was able to note the following points:
1. First, I noticed that the "user-role" field slug, is missing "wpcf-" prefix in the code snippet:
The updated code snippet should be:
add_action('cred_save_data', 'yls_update_user_profile',10,2);
function yls_update_user_profile($user_id,$form_data) {
// if a specific form
if ($form_data['id']==1016)
{
$user_role = $_POST['wpcf-user-role'];
wp_update_user( array( 'ID' => $user_id, 'role' => $user_role ) );
}
}
2. The roles of the slugs of the custom roles will need to be updated, in the "User Role" field's settings, so they match what is saved in the backend:
Screenshot: hidden link
Old values ---> New values
BackOffice ---> backoffice
DispatcherL1 ---> dispatcherl1
Customer ---> customer
3. Even after making the above changes, while editing some specific users through this form, you'll see this error message:
"This form can not edit users with a role of"
It means that an incorrect user role has been assigned to that user already and you'll need to delete that user and add him/her again, to fix this.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar