The gform_address_city Gravity Forms PHP filter is used to modify the “City” label when creating an address city field.
Usage
A generic example of using the filter for all forms:
add_filter('gform_address_city', 'change_address_city', 10, 2);
For a specific form, with form ID 5:
add_filter('gform_address_city_5', 'change_address_city', 10, 2);
Parameters
- $label (string): The label to be filtered.
- $form_id (integer): The current form’s ID.
More information
See Gravity Forms Docs: gform_address_city
Examples
Change the default address city label for all forms
Modify the “City” label to “Town” for all forms.
add_filter('gform_address_city', 'change_address_city', 10, 2); function change_address_city($label, $form_id) { // Modify the city label return "Town"; }
Change the address city label for a specific form
Modify the “City” label to “Municipality” for the form with ID 5.
add_filter('gform_address_city_5', 'change_address_city', 10, 2); function change_address_city($label, $form_id) { // Modify the city label for form 5 return "Municipality"; }
Change the address city label based on form ID
Modify the “City” label for different forms with different labels.
add_filter('gform_address_city', 'change_address_city_based_on_form', 10, 2); function change_address_city_based_on_form($label, $form_id) { // Define custom city labels for specific forms $custom_labels = array( 5 => "Municipality", 6 => "Village" ); // Modify the city label based on the form ID return isset($custom_labels[$form_id]) ? $custom_labels[$form_id] : $label; }
Change the address city label only if it is a specific value
Modify the “City” label if it is “London” to “Greater London”.
add_filter('gform_address_city', 'change_city_label_if_london', 10, 2); function change_city_label_if_london($label, $form_id) { // Check if the current city label is "London" if ($label == "London") { return "Greater London"; } return $label; }
Translate the address city label
Translate the “City” label into another language (e.g., Spanish).
add_filter('gform_address_city', 'translate_city_label', 10, 2); function translate_city_label($label, $form_id) { // Translate the city label to Spanish return __("Ciudad", "my-textdomain"); }