Good morning
I'm trying to pull-of a user-edit form with Ninja Forms on my website.
The logged in user should be able to change his settings. On the registration (also done with Ninja Forms) there's a select field which is mandatory and the value get's stored into a custom field, done with ACF.
If the user want's to edit the account, all fields are pre-populated with other custom fields. That works well, except the select field. I'm really stuck here.
This is the code I'm using
add_filter( 'ninja_forms_render_options', function($options,$settings) {
// Get the user ID
$user_id = get_current_user_id();
// Get the ACF field
$user_custom_field = get_field('user_custom_field', $user_id);
if ($settings['key'] == 'user_custom_field') {
foreach( $options as &$option ) {
if( $user_custom_field == $option[ 'value' ] ){
$option[ 'selected' ] = true;
} else {
$option[ 'selected' ] = false;
}
}
}
return $options;
},10,2);
The code does not work correctly. It always selects the first item in the select list.
question from:
https://stackoverflow.com/questions/65950516/pre-populate-select-field-in-ninja-forms-with-acf-custom-field 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…