Is it possible to use a cred form to create two roles.
I have a registration form that asks for the usual details - name, email etc. Within that form I then need to have a checkbox, which says are you a business or a consumer.
IF they check business they are asked to fill in their Company name and submit. This creates a role called Business
IF they check consumer they are given a message saying you can only purchase certain items. Do you agree YES / No. When they check yes and submit a role is created called Consumer
Thanks
Hi,
If I understand correctly, this CRED form is creating Users. Is that accurate? Typically user roles aren't created by CRED. You will allow users to select from roles you have already created in wp-admin. Unless I am misunderstanding? But it seems unnecessary to use CRED to create roles when there are only 2 to choose from.
Anyway, I would use the CRED API cred_save_data hook to set the correct user role immediately after the user is created. Here's how that would work. First, your CRED form should be set up to create all users in a single role, let's say it's consumer but it doesn't matter which one you choose. Then add a generic field in your CRED form that lets the user choose between Business and Consumer. In cred_save_data you can test the value of that generic field. Depending on the value, you can update the user role using wp_update_user.
CRED generic field information can be found here:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
CRED cred_save_data hook information can be found here:
https://toolset.com/documentation/programmer-reference/cred-api/#csd
Wordpress wp_update_user is documented here:
https://codex.wordpress.org/Function_Reference/wp_update_user
Let me know if you have specific questions about these and I'll be glad to help.
Yes sorry, I probably didn’t explain that quite right. The custom roles already exist. I use the Cred User Form so users can register and be assigned to one of the custom roles.
Would you be able to give me a little more detail as I’m not really a developer, but I can probably muddle through! What code would I need to use in this scenario….
Label - Are you signing up for Business or Personal use?
A Checkbox gives them an option of either Business or Personal. If they check Business, once the form is submitted this adds them to the Business user role. If they check Personal, once the form is submitted this adds them to the Personal user role
Thanks!
Oh, okay I'll help you set this up. et's add the generic field to your New User CRED form. Check out this training course: https://toolset.com/documentation/user-guides/cred-training-course/part-3-adding-generic-fields-conditional-groups-form/
Insert a generic field in your CRED form. I would recommend a "select" or a "radio", not checkboxes. Checkboxes are not the best input types for something that must be either option a or option b. Create an option for each custom role. The value for each custom role should be the slug associated with the custom role - for now, just the lowercase name of the role will be fine.
Save your CRED form and post it up somewhere on your site so I can take a look. We'll go from there.
Thanks Christian - i will do that and give you a shout when ready to look at
Great, let me know when you're ready.
Sorry about the delay still working on it - will give you a shout when done
I understand, thanks. I have marked this ticket as pending an update from you. It will remain open for 30 days pending your response.
Hi Christian, would you be able to set up the private message so i can give you access - thanks!
Private reply fields are enabled now.
- I changed your user role select field name to be something more specific:
[cred_generic_field field='user_select_role' type='select' class='' urlparam='']
- You must add the following code to functions.php:
// Modify new user role based on custom field selection in CRED
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'] == 19872)
{
// modify the user role to match the selected option
$role = $_REQUEST['user_select_role'];
$u = new WP_User( $user_id );
$u->remove_role( 'pending' );
$u->add_role( $role );
}
}
***Note - you should never modify functions.php in your theme, because then updating to a new version of the theme will overwrite the modifications. You should create a child theme and place the code in the child theme's functions.php file. More about child themes:
https://codex.wordpress.org/Child_Themes