NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Oct 09 2022
Estimated reading time : 2 minutes, 36 seconds - 233 words
Share the post "Completing a Selection Field – Formidable Form"
With this tutorial we will show you how to complete a selection field in Formidable Form. Formidable form is one of the best form creation plugin available.
Create a form and add a selection field
You can create a form, configure it and add it where you want it on your WordPress website.
Once the form is configured, you can add the necessary fields. (Note that some fields are only available in the PRO version)
For this example, we will add a custom selection field.
By clicking on our field, we can see its ID on the right-hand side. This ID is important as we’ll use it for certain queries.
Dynamically completing the selection field - our posts
For this query, we will create a frm_setup_new_fields_vars function from Formidable Form.
To edit resulting values, we will add the frm_setup_new_fields_vars filter.
In our function, we will fetch our ID (72) of the field where the results will be displayed; i.e. our posts.
In our function, we created a default label for our selection. (For our example, we’re using Select a question)
label => “Select a question”
add_filter('frm_setup_new_fields_vars', 'themespress_select_field_function', 20, 2); add_filter('frm_setup_edit_fields_vars', 'themespress_select_field_function', 20, 2); function themespress_select_field_function($values, $field){ if($field->id == 72){ $arrayPositions = array(); $pushobj = array( value => "", label => "Sélectionnez une question" ); array_push($arrayPositions, $pushobj); $positionQuery = new WP_Query( array( 'post_type' => 'questions', 'posts_per_page' => -1, 'orderby' => "menu_order", 'order' => "ASC") ); if ( $positionQuery->have_posts() ) { while ( $positionQuery->have_posts() ) { $positionQuery->the_post(); $pushobj = array( value => get_the_title(), label => get_the_title() ); array_push($arrayPositions, $pushobj); } wp_reset_postdata(); } $values['options'] = $arrayPositions; } return $values; }
And finally, in your query, you can adapt whether you want to display posts or pages.
$positionQuery = new WP_Query( array( 'post_type' => 'questions', 'posts_per_page' => -1, 'orderby' => "menu_order", 'order' => "ASC") );