Autocomplete option for taxonomy selection in post_data?
-
Hi – my client is requesting that the “country” (custom taxonomy) selection be an autocomplete form, since there are 254 countries on the list and that’s a lot of scrolling. I agree. I don’t see any options in the settings for this. Do you have a code snippet to make this happen? The same one available for normal ‘single select’ fields.
Thank you very much.
-
Hi @tnightingale,
I hope you are doing well today!
Forminator Address field has built-in search box. You can use it by deactivating all other options except Country.
https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#address-field
https://prnt.sc/cOxyGLw4QD_F
https://prnt.sc/FLk37UFjxlFJKind regards,
ZaferBut this is in a post_data field. I can’t change the type of field for the taxonomy. All I can change is label and description.
Hi @tnightingale,
I am sorry, I didn’t exactly understand where you noticed this issue. Can you please share a screenshot of the same to understand it better?
Kind Regards,
Nebu JohnThis is what the taxonomy selection looks like. You can only scroll, there’s no search or autocomplete.
screenshotHi @tnightingale,
Can you please share an export of the form to examine the issue closely?
Please share the export using pastebin.com or a similar service.
Here is how you can export a Forminator form: https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export
We look forward to hearing back from you.
Kind Regards,
Nebu JohnIt’s not so much an issue as a missing feature.
Here’s the form: https://pastebin.com/HvmN9ssUI’m aware that this is more of a feature thing rather than an issue but being able to check actual form usually helps us better understand the case so thank you for sharing this.
That beings said, I gave it a shot and, as my colleagues already mentioned, this is something that would require custom code. Would you give it a try?:
<?php add_filter( 'forminator_field_postdata_country', 'wpmu_autofill_custom_taxonomy', 10, 1); function wpmu_autofill_custom_taxonomy( $post_field ) { $country_list = forminator_to_field_array( forminator_get_countries_list() ); $select_html = ''; foreach( $country_list as $country ) { $select_html .= '<option value="' .$country['label']. '">' .$country['label']. '</option>'; } $select_html .= '</select>'; $post_field = str_replace( '</select>', $select_html, $post_field ); return $post_field; }To use it on site you need to add it as MU plugin and make a single change in the code.
To add it:
– create an empty file with a .php extension (e.g. “forminator-post-country-autofill.php”)
– copy and paste code into it and save
– upload the file into the “/wp-content/mu-plugins” folder of the site’s WordPress installation on serverThe change:
In this line
add_filter( 'forminator_field_postdata_country', 'wpmu_autofill_custom_taxonomy', 10, 1);you need to change the word “country” in filter name with the actual name of you custom taxonomy. So for example, if the taxonomy name is “country” – it would stay this way; if it’d be e.g. “car” the entire line would change to
add_filter( 'forminator_field_postdata_car', 'wpmu_autofill_custom_taxonomy', 10, 1);I hope it makes sense π
Give it a go, please, and let us know if it works for you.
Best regards,
AdamPretty sure all that will do is replace my custom taxonomy terms with the Forminator country list. That’s not at all what I was asking for.
I’m wanting to show a different user interface for the taxonomy selection field within the post-data field.
How it is now: the person filling out the form has to scroll through a long drop down list of up to 254 countries.
How it should be: they can start typing a country name into a box and select from one or more matches. You have this option for a Select field under Settings: “Search – display the search box in a dropdown”.
Hope that’s clearer. Thank you. I realize I should have titled this “search box in post-data taxonomy field’. Another plugin I use calls this ‘auto complete’.
Pretty sure all that will do is replace my custom taxonomy terms with the Forminator country list.
Yes, that’s exactly what it would do. I assumed your custom taxonomy doesn’t have terms in it (or not all of them) and you just want to make sure that users already have list of countries “out of the box” to choose from.
Sorry for misunderstanding.
The feature that you are after is there for regular select fields but indeed – currently not for select “sub-fields” of Post Data field.
It may be possible to enable it with another bit of custom code but I need to consult this with our developers. I’ve already asked about it and am awaiting their response and we’ll update you here again soon.
Kind regards,
AdamHi @tnightingale,
Could you please try the following snippet and see whether it fits to your needs:
<?php add_filter( 'forminator_field_postdata_category', 'wpmudev_allow_search_taxonomy_data', 10, 1 ); function wpmudev_allow_search_taxonomy_data( $html ) { $html = str_replace( 'type="select"' , 'type="select" data-search="true"', $html ); return $html; }Please do note that in the above code the filter forminator_field_postdata_category the last part i.e. _category is the taxonomy name.
So if you have a different taxonomy name, for example, its country then the filter in your code would change to as follows:
forminator_field_postdata_countryPlease do let us know how that goes. You can implement the above code as a mu-plugins.
Please check this link on how to implement the above code as a mu-plugins:
https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-pluginsKind Regards,
NithinThat works great – thank you so much! Brilliant.
I’ve asked this in other topics but didn’t get a reply yet:
How can I make these filters apply to specific forms only? (by form ID)
Hi @tnightingale,
You can apply in the following format:
<?php add_action( 'forminator_before_form_render', 'wpmudev_search_select_specific_forms', 10, 5 ); function wpmudev_search_select_specific_forms( $id, $form_type, $post_id, $form_fields, $form_settings ){ $form_ids = array( 361 ); if( in_array( intval( $id ), $form_ids ) ) { add_filter( 'forminator_field_postdata_category', 'wpmudev_allow_search_taxonomy_data', 10, 1 ); } } function wpmudev_allow_search_taxonomy_data( $html ) { $html = str_replace( 'type="select"' , 'type="select" data-search="true"', $html ); return $html; }In the above code the following line:
$form_ids = array( 361 );Would point to the Form IDs on which the code would work. If you add your Form ID in there.
Suppose you want to target multiple forms and the from IDS are 123, 345 then the above line will change to:
$form_ids = array( 123, 345 );I hope this clears your query.
Kind Regards,
NithinThank you! That’s the hook I was looking for: forminator_before_form_render
If I wanted to further restrict the filter to specific fields in a form (wouldn’t want to convert a selection field with only 4 choices, for example), could I use the $form_fields variable in a similar way? e.g.:
$my_target_fields = array( 'postdata-1' );
and
if ( in_array( $form_fields, $my_target_fields ) )will that work?
BTW you guys are the best for support!
I hope you are doing well.
We are happy to hear you like the support.
You will need to use $field[‘element_id’]; as it is an array,
https://monosnap.com/file/yXooiebpd6YloElncOdd07NaVs46Xk
So you can do something like:
<?php add_action( 'forminator_before_form_render', 'wpmudev_search_select_specific_forms', 10, 5 ); function wpmudev_search_select_specific_forms( $id, $form_type, $post_id, $form_fields, $form_settings ){ $form_ids = array( 361 ); $field_list = ['text-1', 'text-2']; if( in_array( intval( $id ), $form_ids ) ) { foreach( $form_fields as $field ){ if( in_array( $field['element_id'], $field_list ) ){ add_filter( 'forminator_field_postdata_category', 'wpmudev_allow_search_taxonomy_data', 10, 1 ); } } } } function wpmudev_allow_search_taxonomy_data( $html, $field ) { $html = str_replace( 'type="select"' , 'type="select" data-search="true"', $html ); return $html; }Best Regards
Patrick FreitasThanks!
The topic ‘Autocomplete option for taxonomy selection in post_data?’ is closed to new replies.