Skip Navigation

[Resolved] Change user role after CRED post form

This support ticket is created 5 years, 4 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Jaime 5 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#1320427

Tell us what you are trying to do?
I would like to change the role of a user after complete one Post form.
More detailed:
After one user complete the form with ID 2251 it should change the role from 'pendiente' a 'guia_produccion'.

Is there any documentation that you are following?
Yes, I've tried those topics, but I'm not be able to reach it.
https://toolset.com/forums/topic/change-user-role-with-cred-form/
https://toolset.com/forums/topic/need-to-change-roles-via-creds-there-is-no-role-field-in-toolset-cred/
https://toolset.com/forums/topic/change-a-users-role-upon-submission-of-a-form/

Including those codes (which I changed as far as my knowledge let me do) :

1)

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'] == 2320) {
// modify the user role to match the selected option
$role = get_user_meta( $user_id,'wpcf-user_select_role', true );
$u = new WP_User( $user_id );
$u->remove_role( 'pending' ); /// adjust role name here if needed
$u->add_role( $role );
}
}

2)

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{

if ($form_data['id']=='279')
{
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
wp_update_user ( array ( $user_id, 'role' => 'inventory_admin' ));
}
}

Is there a similar example that we can see?
Not sure.

What is the link to your site?
hidden link

Can you help me Shane?
🙂

#1320583

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jaime,

Thank you for getting in touch.

Actually the correct way of doing this is the following.

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'] == 2320) {
// 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( 'pending' ); /// adjust role name here if needed
$u->add_role( $role);}
}
}

This should get it working.

However is the user role field on the user a select field ?

Please let me know.
Thanks,
Shane

#1320995

Sorry Shane.
Actually my Issue is resolved (great! thanks a lot!) but I wanted to ask you to confirm my code is correct, because I changed two things and besides it's working fine, I would like a pro confirmation.

where your code says :

$u->remove_role( 'pending' ); /// adjust role name here if needed
$u->add_role( $role);}

I put this :

$u->remove_role( 'pendiente' ); /// adjust role name here if needed
$u->add_role( 'guia_produccion' );}

It's working as I expected, but is it the code correct?

Thanks a lot Shane.
You saved me.

#1321225

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jaime,

Yes

$u->remove_role( 'pendiente' ); /// adjust role name here if needed
$u->add_role( 'guia_produccion' );}

This is correct because you need to remove the previous role first and then add the new role to the user.

Thanks,
Shane

#1321299

My issue is resolved now. Thank you!