NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Create a User Status With Formidable Form
Intermediate

---- Last modified : Nov 18 2021

Estimated reading time : 3 minutes, 17 seconds - 325 words

With this tutorial we will show you how to create a function that will allow users to chose their roles based on a pre-determined list and subsequently create a new role for the user.

First we need to create our user role. In this example, we want to add a Doctor role, Pharmacist role and a Nurse role; via the functions.php or another file.

//////////////////////// Add User Role /////////////////////// 
function medecin_new_role() {  
 
    //add the new user role
    add_role(
        'medecin',
        'Medecin',
        array(
            'read'         => true,
            'delete_posts' => false
        )
    );
 
}
add_action('admin_init', 'medecin_new_role');
 
function pharmacien_new_role() {  
 
    //add the new user role
    add_role(
        'pharmacien',
        'Pharmacien',
        array(
            'read'         => true,
            'delete_posts' => false
        )
    );
 
}
add_action('admin_init', 'pharmacien_new_role');
 
function infirmiere_new_role() {  
 
    //add the new user role
    add_role(
        'infirmiere',
        'Infirmièr(e)',
        array(
            'read'         => true,
            'delete_posts' => false
        )
    );
 
}
add_action('admin_init', 'infirmiere_new_role');

We can now validate a user’s profil and confirm that our three newly added roles are listed. Then we have to create a form that will allow someone to create their new user with the formidable User Registration add-on.

Simply create a form with the necessary fields such as first/last name, email, etc… For this to work, make sure to create a Statut field (you can name it however you see fit). For this field, make sure it’s a Radio button with the three roles we mentioned above for the example:

Now we will need to go into the form’s settings under Actions/Notifications to add a user registration function. Fill out the necessary and leave author by default.

For example:

Voici la fonction qui donne les conditions nécessaires d’attribuer le bon rôle utilisateur au moment de son inscription et de son choix de rôle. Here’s the function that will return the necessary conditions in order to assign the right role to the user when signing up and choosing their role.

We change the IDs by the appropriate IDs of your form and field.

  • ID 190 is the field ID.
  • ID 6 is the form ID.
//////////////////////// Formidable form attribute user rôle /////////////////////// 
function statut_new_role($user_role, $atts){
  extract($atts);
  if($form->id == 6){ // WSIS: change this to your form ID
    if($_POST['item_meta'][190] == 'medecin') //change 125 to the ID of your field and 'Author' to the value of your radio button WSIS: or single checkbox
    $user_role = 'medecin'; // WSIS: rolename
	if($_POST['item_meta'][190] == 'infirmiere') //change 125 to the ID of your field and 'Author' to the value of your radio button WSIS: or single checkbox
    $user_role = 'infirmiere'; // WSIS: rolename
	if($_POST['item_meta'][190] == 'pharmacien') //change 125 to the ID of your field and 'Author' to the value of your radio button WSIS: or single checkbox
    $user_role = 'pharmacien'; // WSIS: rolename
  }
  return $user_role;
}
 
add_filter('frmreg_new_role', 'statut_new_role', 10, 2);

Finally, you will need to test the form to confirm that it will assign the selected tole to your user. Make sure that all data are displayed individually in your form and that in your conditional function, it displays the proper value assigned to each role.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>