I am trying to:
I inserted this code provided by Shane, to change the role of users after uploading a form.
-> My previous ticket: https://toolset.com/forums/topic/change-user-role-after-cred-post-form/
-> My code:
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'] == 2253) {
// modify the user role to match the selected option
$user = wp_get_current_user();
$role = get_user_meta( $user->ID,'wpcf-user_select_role', true );
if ( !in_array( 'admin', (array) $user->roles ) ) {
$u = new WP_User( $user->ID );
$u->remove_role( 'pendiente' ); /// adjust role name here if needed
$u->add_role( 'guia_produccion' );}
}
}
It's working fine.
But I now I need to do the same thing with another role and another form and when I insert the code, it gives me an error
"This site is experiencing technical difficulties".
-> My new code
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'] == 4342) {
// modify the user role to match the selected option
$user = wp_get_current_user();
$role = get_user_meta( $user->ID,'wpcf-user_select_role', true );
if ( !in_array( 'admin', (array) $user->roles ) ) {
$u = new WP_User( $user->ID );
$u->remove_role( 'inactivo_localizaciones' ); /// adjust role name here if needed
$u->add_role( 'propietario' );}
}
}
What I'm doing wrong?
Thanks a lot!
🙂
Hi, the first thing to note is that you cannot repeat function names. So if you are trying to include both these custom code snippets in your site, you will experience an error. You should change the function name in one of the snippets. You should replace cred_update_user_role_action twice in the new code snippet - once in the function name, and another time in the add_action line. The function names must match, and must not conflict with other function names on your site.
My issue is resolved now. Thank you Christian!