Change current user role with form selection on update (not entry creation)

I’m using Formidable forms in WordPress and have a form that registers users. I can use a radio button in the registration form to determine what their role will be. I have a hook for that. What I need, however, is a hook that will change the user role based on radio selection on form entry UPDATE. My current code only works on entry creation. Here is the code that assigns roles on registration:

add_filter('frmreg_new_role', 'frmreg_new_role', 10, 2);

function frmreg_new_role($role, $atts){
  extract($atts);
  if($form->id == 8){
    if($_POST['item_meta'][280] == 'Job Applicant')
      $role = 'applicant';
  }
  return $role;
}

“8” is the id of the form itself. “280” is the id of the radio button field where “Job Applicant” is one of the values. And “applicant” is one of our site’s user roles.

Read More

I need an adaptation of this that will change the role after the entry has already been created, on update. The closest thing I can find is a hook that changes user role after a successful PayPal payment. I tried to combine the two but I couldn’t get it to work. Here is the PayPal generated user role changer:

add_action('frm_payment_paypal_ipn', 'change_paid_user_role');

function change_paid_user_role($args){
    $new_role = 'contributor'; //change this to the role paid users should have

    if(!$args['pay_vars']['completed'])
       return; //don't continue if the payment was not completed

    if(!$args['entry']->user_id or !is_numeric($args['entry']->user_id))
       return; //don't continue if not linked to a user

    $user = get_userdata($args['entry']->user_id);
    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);
}

UPDATE: the action hook should probably be: frm_after_create_entry according to Formidable forums.

Related posts

Leave a Reply

1 comment

  1. Many times, researching the core files is more productive than any Google or Manual. Dropping the whole plugin directory in a code editor and researching for the string frm_after_create_entry takes us to the create() method where this hook happens.

    After that, there’s the update() method and it provides the action hook: frm_after_update_entry.

    This hook passes two parameters: $id and $new_values['form_id']. I cannot reproduce your setup, so testing the hook is up to you.

    Reference: Actions and filters are NOT the same thing…

    In this example:

    add_action( 'frm_after_update_entry', 'change_role_to_staff', 10, 2); 
    
    function change_role_to_staff( $form_id, $values ){ 
        var_dump($values);
        die();
    }
    
    • As this is an action hook, nothing has to be returned.
    • There’s no $roles or $atts, the parameters are the form ID and Values.
    • What you’re looking for is inside $values.
    • var_dump() and die() are for debugging purposes and must be removed at once after testing.
    • Do your wp_update_user with this values and adapting your previous code.