Dynamically add options to a 'Select' field and ensure that they show when editing a Submission.
I’ve written before about dynamically setting the options in a Ninja Forms form drop down (and for WooCommerce variable products). I noticed two problems when doing this – the chosen option is not shown in the Submissions area nor in the {all_fields_table}.
{all_fields_table} bug
Using sample code from the Ninja Forms codex, I have reported this issue as a bug.
There is a workaround – use the {field:xxxx} shortcode in the form’s Email Confirmation, Email Notification and Success Message actions.
Fix for Submissions
When you add a ‘Select‘ field it comes with 3 options – One, Two and Three. You can change, delete or add to these.
The code from the codex demonstrates how to add to or replace these options. For this demo I add one new option: ‘New Seven‘ with value ‘new‘.
The problem happens when you choose the ‘New Seven‘ option and submit the form. The value (‘new‘) will be stored in the database and can be included in a Success Message, Email Confirmation or Email Notification but it will not be listed when you edit the Submission.
When you edit a submission it uses the form as designed i.e. the fields are listed in the order that they are in the form.
The ‘Select‘ field’s options are taken from the form at this moment in time. This means that if the value chosen when the form was submitted is no longer in the form, it will be displayed as a blank! The chosen value is stored in the database (though the label that was displayed isn’t).
Solution: Dynamically add chosen value if necessary
I generally only use the Submissions area as a backup in case of Email Notification issues. Despite this, it has frustrated me for the last 7 months that the chosen value isn’t displayed despite being available in the database. After a lot of reading of the Ninja Forms source code and reading database exports I found that the submission’s post ID was available in $_GET[ ‘post’ ] and the field order was in $settings[ ‘order’ ]. I passed these to get_post_meta() to retrieve the value that was chosen when the form was submitted.
I extended the the example in the codex to retrieve the chosen value from the post_meta table and add it to the list of options if it isn’t already present.
I have updated the code in my other posts so that they can finally display the dynamically added value when viewing the submission. This is quite a relief!
Hi, i just saw your post today.
I would like to know if i can show an array of values in another empty list field depending on the choice of the user in one list field, just like a state list field and cities of these states list field.
Let me know. Thanks in advance and thanks for your code ;)
@Benjamín: I think that this would require some JavaScript and the data could not be added before the form is displayed (like how I generated a list of product variations in Product Enquiry form with Ninja Forms)
I think that you should look at the JavaScript hooks that Ninja Forms provides. https://developer.ninjaforms.com/codex/javascript-overview/ I have not used it. The GitHub example is 5 years old but it might be worth trying it out.
Otherwise you could write basic JavaScript that watches the state field and loads the cities field when state changes.
I’ve got fields coming in dynamically and being displayed within the form… however, they are not being submitted. Any idea why this might be? The filter is being triggered from my custom plugin, not functions.php.
@Devin – Your filter should work – there is no difference to WordPress whether a plugin or theme triggers it. Are you able to email me the code? I can test locally and debug the issue.
Are you using ninja_forms_render_options filter? Are the fields in the database? You saw that there is a bug in {all_fields_table} where custom dropdown values are not displayed. The workaround is to use the {field:xxxx} token.
Devin replied to my email with: “I discovered yesterday that the data is being saved, but it’s not showing in the Submission screen, which might be what you’re referring to? But here is my code anyway:”
add_action( 'nf_get_form_id', function( $form_id ) { // Check for Request Meeting Form ID 5 if ( 5 !== $form_id ) return; add_filter( 'ninja_forms_render_options', function( $options, $settings ) { global $post; $options_before = []; $options_after = [];
$pain_points = get_field('pain_points', $post->ID); if ( 'pain_points_1680032260551' == $settings[ 'key' ] ) { if ($pain_points) { foreach( $pain_points as $key => $value) { $options_before[] = [ 'label' => $value['pain_point'], 'value' => $key, 'calc' => 0, // 'selected' => is_admin() ? false : true, 'selected' => false, ]; } } }
return array_merge($options_before, $options, $options_after); }, 10, 2); } );
@Devin: Is get_field() the Advanced Custom Fields function? Is it returning the expected value?
Could you try moving the add_filter() call to be outside of the nf_get_form_id hook, in case that is not being called when viewing a submission.