Skip Navigation

[Resolved] Change user role after CRED post form

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

Problem: I copied some code I already had in place and made some modifications, but it causes an error and I can't figure out why.

Solution: If you reuse the same function names you will experience an error. Be sure the function names are unique.

This support ticket is created 5 years, 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Jaime 5 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1376253

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!
🙂

#1376355

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.

#1376485

My issue is resolved now. Thank you Christian!