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'.
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)
{
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 ?
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?