Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

php - Pre-Populate select field in Ninja Forms with ACF custom field

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...