Role change after submission for Pionetforms

47 Views Asked by At

I would like to set up if subscribers submit a form then the role is changed from subscriber to customer.

For your reference, I am sharing with you the Pionetforms' action hook and filter hook: https://piotnetforms.com/docs/actions-hook-filter-hook/

I have googled and found some code sources, but I have no clue how to work with Pionetforms.

    add_action('cred_save_data', 'change_user_role_on_save',10,2);
function change_user_role_on_save($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==573)
    {
     $user = wp_get_current_user();
   
       
if ( in_array( 'subscriber', (array) $user->roles ) ) {
         
// Remove role
$user->remove_role( 'subscriber' );
  
// Add role
$user->set_role('customer');
}
          
    }
}

Currently using "members" plugin which is made by memberpress.

I would be grateful if anyone let me know how to solve it. Thank you.

1

There are 1 best solutions below

0
intLab On

Here is a code:

You can chagne formname1, formname2, formname3, formname4

  function piotnet_change_role_user($form_submission){
        $form_id = $form_submission['form']['id'];
        if(in_array($form_id, ['form_name1', 'form_name2', 'form_name3', 'form_name4'])){
            $email = $form_submission['fields']['email']['value'];
            $user = get_user_by_email($email);
            if($user){
                if(in_array($form_id, ['form_name1', 'form_name2'])){
                    $user->remove_role( 'subscriber' );
                    $user->add_role( 'customer' );
                }else{
                    $user->remove_role( 'subscriber' );
                    $user->add_role( 'customer' );
                    $user->add_role( 'contributor' );
                }
            }
        }
     }
    
    add_action('piotnetforms/form_builder/new_record_v2', 'piotnet_change_role_user');