Skip Navigation

[Resolved] Edit user roles from CRED Form

This support ticket is created 5 years, 9 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: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by temel-tecnicas-deM 5 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#1229161
Captura.PNG

I am trying to: make a "Employee Manager" for our Intranet, everything works well, but we wants to be able to edit any role from the Employee Manager section.
In this section we have a list of all Employees with the department they belong to, and we edit the users in a modal form that redirects to the same place updating the view with the employees.
I tried this code but i doesnt seem to work.

add_action('cred_save_data', 'cred_update_user_role_action',10,2);
function cred_update_user_role_action($user_id, $form_data) {

if ($form_data['id'] == 4179) {
// modify the user role to match the selected option
$role = get_user_meta( $user_id,'wpcf-rol-intranet', true );
$u = new WP_User( $user_id );
$u->remove_role( 'empleado' ); /// adjust role name here if needed
$u->add_role( $role );
}
}

#1229400

Hi there,

Thank you for contacting us and I'd be happy to assist.

Instead of using the "WP_User" class directly, I'll recommend using the "wp_update_user" function, to update the user role.
( ref: https://codex.wordpress.org/Function_Reference/wp_update_user )

The following reply includes a working code example:
https://toolset.com/forums/topic/cant-update-user-role-from-cred-form/#post-503190

I hope this helps.

regards,
Waqar

#1230890

My issue is resolved now. Thank you!