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]
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.
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']);
}
}
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 );
}
}
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?
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.
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
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);
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
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).
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
Aaah that makes sense 🙂 thanks a lot it works perfectly now 🙂
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.