Skip Navigation

[Resolved] Radio buttons selection triggers assigning to one of 2 user roles

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a CRED form that creates new Users in a custom role with slug "kunde". I have defined two custom roles that I would like the User to be able to select from, and when the form is submitted I would like to change their role to the selected role.

Solution: Use a generic field to allow the user to select a role. The value for each option should be the slug of your custom role.

[cred_generic_field field='user_select_role' type='radio' class='zbrole' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[
{"value":"kunde_mit_zentralbelieferung","label":"Kunde mit Zentralbelieferung"},
{"value":"kunde_ohne_zentralbelieferung","label":"Kunde ohne Zentralbelieferung"}
]
}
[/cred_generic_field]

Use cred_save_data to modify the User's role after the User is created:

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'] == 14)
  {
  // modify the user role to match the selected option
    $role = $_REQUEST['user_select_role'];
    $u = new WP_User( $user_id );
    $u->remove_role( 'kunde' ); //cred form role setting
    $u->set_role( $role );
  }
}

Relevant Documentation:
https://toolset.com/documentation/user-guides/creating-cred-forms/
https://toolset.com/documentation/user-guides/cred-user-forms/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

100% of people find this useful.

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 2.33 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#554905

I am trying to implement a radio select in a cred form that creates a new user. I have tried following this thread https://toolset.com/forums/topic/choose-user-role-on-registration/ but to no avail :/

Depending on the selection of a radio button, a certain role should be assigned. This is my current code:

functions:

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'] == 14)
  {
  // modify the user role to match the selected option
    $role = $_REQUEST['wpcf-user_select_role'];
    $u = new WP_User( $user_id );
    $u->remove_role( 'kunde' );               // cred form role setting
    $u->set_role($_POST['wpcf-user_select_role']);
  }
}

Radio in form:

[cred_generic_field field='user_select_role' type='radio' class='zbrole' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[
{"value":"1","label":"Kunde mit Zentralbelieferung"},
{"value":"2","label":"Kunde ohne Zentralbelieferung"}
]
}
[/cred_generic_field]

#554923

Hi, first please check the values of each option in the generic field.

...
{"value":"1","label":"Kunde mit Zentralbelieferung"},
{"value":"2","label":"Kunde ohne Zentralbelieferung"}
...

Your values are "1" and "2", which seems a bit odd. These values should be identical to the slugs of each custom Role you would like the user to select. Most of the time, roles are not numbers, they are text strings. So if your custom roles are "teacher" and "student" then your values should be "teacher" and "student" instead of "1" and "2". If your custom roles are "Kunde mit Zentralbelieferung" and "Kunde ohne Zentralbelieferung", then the slugs are probably "kunde-mit-zentralbelieferung" and "kunde-ohne-zentralbelieferung".

This is what I would try first, then if still not working we can take a closer look.

#555018

Oh ok that makes sense I got that mixed up, it still isnt working though. Attached is a screenshot of the roles and here is the code. I tried with and without hyphens in the names of the roles:

[cred_generic_field field='user_select_role' type='radio' class='zbrole' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[
{"value":"kunde-mit-zentralbelieferung","label":"Ja"},
{"value":"kunde-ohne-zentralbelieferung","label":"Nein"}
]
}
[/cred_generic_field]

Is it possible that the error lies within the function maybe, especially that line to set role?

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'] == 14)
  {
  // modify the user role to match the selected option
    $role = $_REQUEST['wpcf-user_select_role'];
    $u = new WP_User( $user_id );
    $u->remove_role( 'kunde' );               // cred form role setting
    $u->set_role($_POST['wpcf-user_select_role']);
  }
}

#555237

Yes, I think you must also change the callback function. The wpcf- prefix isn't necessary for a generic field name, and you can clean up the role variable assignment like this:

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'] == 14)
  {
  // modify the user role to match the selected option
    $role = $_REQUEST['user_select_role'];
    $u = new WP_User( $user_id );
    $u->remove_role( 'kunde' );               // cred form role setting
    $u->set_role( $role );
  }
}
#555253

Somehow it just doesnt assign any role at all, when I go to the user overview ist says none in the column showing the role.

Could it be something about how I name the values in the radio button?

The actual names are:
"Kunde mit Zentralbelieferung" & "Kunde ohne Zentralbelieferung"

I tried all lowercase, normal case with and without hyphens and all the possibilities I could think off :/ any other ideas of what might prevent this from working?

#555292

Okay perhaps something else is going on. Let's enable server logging to get some more information. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Next, modify your functions.php file to include some logging:

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'] == 14)
  {
  error_log('form id 14');
  // modify the user role to match the selected option
    $role = $_REQUEST['user_select_role'];
    error_log('role: ' . $role);
    error_log('post role: ' . $_POST['user_select_role']);
    error_log('user id: ' . $user_id);
    $u = new WP_User( $user_id );
    $u->remove_role( 'kunde' );               // cred form role setting
    $u->set_role( $role );
  }
}

Then submit your CRED form once again. You should then find an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the updates you made to wp-config.php and remove the 4 error log statements from functions.php.

Edited to correct log typo.

#555296

I get an error after adding the code to the functions.php

Parse error: syntax error, unexpected '$u' (T_VARIABLE) in /home/marti751/public_html/cup/wp-content/themes/bb-theme-child/functions.php on line 56

it refers to this line:

$u = new WP_User( $user_id );

Tried switching the = for -> and a few other things but no luck

#555319

I made a typo, my apologies. Please update the error log for user id to include a closing parentesis and semi colon like so:

error_log('user id: ' . $user_id);
#555323

This is weird I mean I am no export on error logs but it all seems perfectly fine see the error log attached. Should I create FTP access for you to check? Thanks 🙂

the error log only says:

[01-Aug-2017 17:04:33 UTC] form id 14
[01-Aug-2017 17:04:33 UTC] role: Kunde mit Zentralbelieferung
[01-Aug-2017 17:04:33 UTC] post role: Kunde mit Zentralbelieferung
[01-Aug-2017 17:04:33 UTC] user id: 30

#555330

It appears that the selected option has a value of "Kunde mit Zentralbelieferung" but it should instead be the slug of this role. Slugs do not have caps or spaces. Sure, I can take a look in wp-admin. I have enabled private reply fields here. Please also let me know where I can find the form on the front-end of your site (what page or post URL).

#555349

Ah, you should replace the spaces with underscores instead of hyphens. For future reference, you can find the slug of a role by going to Toolset > Access Control and clicking the "View users" link underneath the role name. The role slug will appear in the URL as a role parameter and value:
.../wp-admin/users.php?role=kunde_mit_zentralbelieferung

#555364

Aaah that makes sense 🙂 thanks a lot it works perfectly now 🙂

#555365

Ok great, don't forget to revert any changes you made to wp-config.php and remove all error log statements from the code in functions.php - there's no reason to leave these in place now.