How to Add Search to Drop Down Lists

On many of the projects we build, one request that keeps coming up is the ability to add in a search feature to drop downs. This is almost critical when the drop down is dynamically populated and there are hundreds of choices (selecting countries for example).

The Code

add_action("gform_pre_render", "set_chosen_options");
function set_chosen_options($form){
    ?>
    <script type="text/javascript">
    gform.addFilter('gform_chosen_options', 'set_chosen_options_js');
    function set_chosen_options_js(options, element){
        options.search_contains = true;
        return options;
    }
</script>
    <?php
    //return the form object from the php hook
    return $form;
}

How To Use

The above code should be placed in functions.php of your WordPress theme. You will also need to ensure that the Enhanced Interface Option is turned on. This can be found under appearance for the Drop Down in the form you are editing.

Good practice would be to create a child theme to allow this code be entered without fear that it will be erased when you update your main theme.