Action to change role on update form

By: Yannick Berges | Asked: 10/14/2025
ForumsCategory: Code SnippetsAction to change role on update form
Yannick Berges asked 4 weeks ago

hello
i have 3 roles in my site
subscrirer
contributor
valide

i need to update role when my user update/create entry
i have a field (id 19 radio Oui/Non) inside this form and role is depend to this

i add this code in function

/**
* This will change an inactive user to a member after they complete their member profile.
*/
add_action('frm_after_create_entry', 'inactive_to_member', 20, 2);
function inactive_to_member($entry_id, $form_id){
 if($form_id == 4){ //my form id
   $new_role = ''; //change this to the role users should be granted upon completing form
$handicap = $_POST['item_meta'][19];
if ($handicap == 'Oui') {
$new_role = "contributor";
} elseif ($handicap == 'Non'){
  $new_role = "valide";
}
   $user = wp_get_current_user(); //get logged in user
error_log( $user );
error_log( $new_role );
   if(!$user) {
       return; //don't continue if user doesn't exist
   }
   $updated_user = (array)$user;
   // Get the highest/primary role for this user
   $user_roles = $user->roles;
   $user_role = array_shift($user_roles);
   if ( $user_role == 'administrator' )
       return; //make sure we don't downgrade any admins
   $updated_user['role'] = $new_role;
   wp_update_user($updated_user);
 }
}

But nothing was happend and i don't understand why i have anything in error_log
thanks for any help ?
regards

2 Answers
Rob LeVineRob LeVine Staff answered 4 weeks ago

Based on the code above, ff there's nothing in your WP Debugging log file, either WP Debugging isn't turned on correctly or the ID of the form you're interested in isn't 4. You'll have to dig deeper to find the root of the current issue.

Yannick Berges
replied 4 weeks ago

hello thanks for your return
i found
need to add create and update
add_action('frm_after_create_entry', 'inactive_to_member', 20, 2);
add_action('frm_after_update_entry', 'inactive_to_member', 20, 2);

Victor Font Staff answered 4 weeks ago
frm_after_create_entry should fire with a priority of 30, not 20. You may be firing it too early in the process. See this: https://formidableforms.com/knowledgebase/frm_after_create_entry/

I would also add some additional logging or debugging messages to your code.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right