• Resolved johnchanmk

    (@johnchanmk)


    I am trying to add options at a list dropdown dynamically at functions.php of the theme using ninja_forms_field (http://docs.ninjaforms.com/article/146-ninjaformsfield) . But what should be the key of that part of the array controlling the dropdown options? I try print_r($data) and seems cannot get the key like [label], etc

    [label] => Package
    [input_limit_msg] => character(s) left
    [label_pos] => above
    [list_type] => dropdown
    [multi_size] => 5
    [list_show_value] => 0
    
    => Array
    (
    [options] => Array
    (
    [0] => Array
    (
    [label] => Option 1
    [value] =>
    [calc] =>
    [selected] => 0
    )
    )
    )
    
    [user_info_field_group] =>
    ......

    https://ww.wp.xz.cn/plugins/ninja-forms/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello! Thanks for using Ninja Forms. You can easily use that hook to use HTML to dynamically add options to the dropdown list. Where are you getting the data to add to the dropdown list?

    Thread Starter johnchanmk

    (@johnchanmk)

    Thanks. I just extract the data from postmeta from wordpress and it could be done successfully. So what should be the way to insert? is it like

    $data['options'] = array(
            array('label'=>'Option 2'),
            array('label'=>'Option 3')
    );

    ?

    Well, I have some sample code for you to inspect and modify based on what you need to accomplish:

    function my_filter_function( $data, $field_id ){
    // $data will contain all of the field settings that have been saved for this field.
    // Let's change the default value of the field if it has an ID of 148
       if ( $field_id == 148 ) {
            if ($data['label'] == 'List') {
                $data['list']['options'] = array(
                    array('label' => 'Nissan', 'value' => null, 'calc' => null, 'selected' => 0),
                    array('label' => 'Kia', 'value' => null, 'calc' => null, 'selected' => 0)
                    );
            }
        }
        return $data;
    }
    add_filter( 'ninja_forms_field', 'my_filter_function', 10, 2 );
    Thread Starter johnchanmk

    (@johnchanmk)

    Thanks! the code works well

    Thread Starter johnchanmk

    (@johnchanmk)

    Furthermore about the question. I tried to create a dynamic list, and how to save the selected option back and display in the backend? It seems that the plugin does not save the value.

    Furthermore, I would like to save the value of a field that the value is changed by jquery, is it possible? Thank you for the support.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Set List options dynamically?’ is closed to new replies.