Get list of countries using gravity form plugin

Sometimes, we need to show list of countries in custom form or in simple page. Writing each country name in file is very tedious. If we have installed gravity form in our site then we can create and show dropdown of countries easily with help of gravity form plugin.

In gravity form plugin, there is a class GF_Field_Address in which address fields are created. In this class there is a function get_countries that returns the arrray of countries added in gravity form. We can use this class and method to create our country dropdown in custom form which is not a gravity form.

Thus to create dropdown of country, we have to just write below code :

<select class=”selectpicker” name=”country” >
<?php $countries = GF_Field_Address :: get_countries();
if(!empty($countries) && count($countries) > 0){
foreach($countries as $country){
echo ‘<option value=”‘.$country.'” ‘.(($country==’Canada’)?’selected=”selected”‘ : ”).’>’.$country.'</option>’;
}
}
?>
</select>

If we just want to display, list of country, then we can do as :

<?php $countries = GF_Field_Address :: get_countries();
if(!empty($countries) && count($countries) > 0){
foreach($countries as $country){
echo ‘<li>’.$country.'</li>’;
}
}
?>

Hope, it will save your valuable time…

Leave a Reply