I am trying to limit the country field in the address portion of a form to only two countries: United States and Canada.
I have found where we can limit Gravity Forms to certain countries. I don’t want to do that as I want the other county options available on other forms. I only want to limit the options on a single form.
Thoughts?
Give the following a try. I think this should limit the countries, but should only apply the filter when loading a form with the specified ID.
add_filter( 'gform_pre_render_77', 'typewheel_limit_countries' );
add_filter( 'gform_pre_validation_77', 'typewheel_limit_countries' );
add_filter( 'gform_pre_submission_filter_77', 'typewheel_limit_countries' );
add_filter( 'gform_admin_pre_render_77', 'typewheel_limit_countries' );
function typewheel_limit_countries( $form ) {
add_filter( 'gform_countries', function( $countries ) {
return array( 'Brazil', 'United States', 'Netherlands', 'United Kingdom' );
});
return $form;
}
Joshua,
Thanks for the suggestion.
Am I correct that these edits should be placed in the function file for the theme?
If so, I added and made a change from the 77 in your code to 7 which is the ID of the specific form. However, no changes appeared in the form or on the form page.
Yes, you can place this in your functions.php
file. Make sure all four instances of the form ID have been replaced. Also, there is no caching that might be loading an old version of the page, is there? I just tested on my local environment and it did work properly.
I forgot I was running Code Snippets plugin. I created a new snippet with your code instead of placing in the Functions file. And it works.
Thanks.