PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Abbreviate City Fields

  1. Hi,

    I would like to abbreviate the "City" field value so that when it is passed through with the mailchimp integration, it is "Fl" instead of "Florida" for example....

    Thanks!

    Posted 14 years ago on Wednesday February 17, 2010 | Permalink
  2. Brian, are you referring to the "State" drop down in the Address field? I ask because you reference a state but the post talks about Abbreviating City Fields...

    Posted 14 years ago on Wednesday February 17, 2010 | Permalink
  3. I just needed to do the same thing for a site I'm working on. If you use the advanced fields option where it automatically adds in all of the address fields then trying to modify the states dropdown doesn't work (at least I couldn't get it to). My solution requires modifying the core code of Gravity Forms but it's easy to do. I needed a form to work with PayPal which requires the state to be passed using the USPS official abbreviations if you want to prepopulate the billing info. fields. Here's how you do it.

    1. Open up common.php in the Gravity Forms plugin folder.
    2. At line 967 you'll find public static function get_us_states(
    That's the part that sets up the values that show in the states drop down menu.
    3. Right after it (after the } that ends that function) paste in the following:

    public static function get_my_us_states(){
    return array("Alabama"=>"AL","Alaska"=>"AK","Arizona"=>"AZ","Arkansas"=>"AR","California"=>"CA","Colorado"=>"CO","Connecticut"=>"CT","Delaware"=>"DE","District of Columbia"=>"DC","Florida"=>"FL","Georgia"=>"GA","Hawaii"=>"HI","Idaho"=>"ID","Illinois"=>"IL","Indiana"=>"IN","Iowa"=>"IA","Kansas"=>"KS","Kentucky"=>"KY","Louisiana"=>"LA","Maine"=>"ME","Maryland"=>"MD","Massachusetts"=>"MA","Michigan"=>"MI","Minnesota"=>"MN","Mississippi"=>"MS","Missouri"=>"MO","Montana"=> "MT","Nebraska"=>"NE","Nevada"=>"NV","New Hampshire"=>"NH","New Jersey"=>"NJ","New Mexico"=>"NM","New York"=>"NY","North Carolina"=>"NC","North Dakota"=>"ND","Ohio"=>"OH","Oklahoma"=>"OK","Oregon"=>"OR","Pennsylvania"=>"PA","Rhode Island"=>"RI","South Carolina"=>"SC","South Dakota"=>"SD","Tennessee"=>"TN","Texas"=>"TX","Utah"=>"UT","Vermont"=>"VT","Virginia"=>"VA","Washington"=>"WA","West Virginia"=>"WV","Wisconsin"=>"WI","Wyoming"=>"WY");
    }

    4. Just a bit below that you'll see the following code: public static function get_us_state_dropdown(
    Change the name of that function to get_us_state_dropdown_old

    5. Paste in the following code below it:

    public static function get_us_state_dropdown($selected_state = ""){
    $states = self::get_my_us_states();
    foreach($states as $state => $abbr ){
    $selected = $abbr == $selected_state ? "selected='selected'" : "";
    $str .= "<option value='" . esc_attr($abbr) . "' $selected>" . $state . "</option>";
    }
    return $str;
    } return $str;
    }

    Voila, your address fields now have the full name of the state showing in the drop down but use the postal abbreviation for the value.

    Please note that this will be the case for all forms you create in the future. If you want to go back to the stock values, simply rename the new function to _old and remove the _old from the original function. Also note that the state abbreviation is what will be stored in the database -- which works great for my needs but might not be what you want.

    Cheers!

    Posted 14 years ago on Thursday February 18, 2010 | Permalink
  4. Sorry Carl, yes, I meant "State" fields, not city fields. Oops....I guess that was the Irishman living in America talking for a minute!

    Newport, awesome, I'm going to try that now.

    Thanks very much!

    Posted 14 years ago on Thursday February 18, 2010 | Permalink
  5. Great stuff, worked perfectly! Thanks for your help, much appreciated!

    Posted 14 years ago on Thursday February 18, 2010 | Permalink
  6. This is how I anticipated having to fix it, but was worried that I would break any future updates to the plugin.

    Is there a hook or something else we can use to modify the submitted data instead of hacking the plugin?

    Posted 14 years ago on Tuesday March 30, 2010 | Permalink
  7. Right now there isn't a hook for this specific change. But it is something we will look at adding in the next release if we can work it in. So keep track of this change if you go ahead and make it.

    Down the road we do plan on implementing the ability to select an option to use abbreviations for the value. This will be implemented when we implement the ability to have control over both the value and label for drop downs, check boxes, and radio buttons.

    Posted 14 years ago on Tuesday March 30, 2010 | Permalink
  8. I just used gform_pre_submission to find the address field and convert the long name to a short name and assign it to the post data before it is saved and is working good.

    With permission from my client, I will post the code here.

    Posted 14 years ago on Tuesday March 30, 2010 | Permalink
  9. Here we go. I created a custom programing plugin and added this to it which will convert the long name to short name before it is posted/emailed.

    [php]
    <?php
      /*
        Plugin Name: Custom Programming
        Plugin URI: http://www.example.com/
        Description: Plugin for altering gForms state data
        Author: Matthew Connerton
        Version: 1.0
        Author URI: http://www.example.com/
      */
    add_action("gform_pre_submission", "pre_submission_state_handler");
    function pre_submission_state_handler($form_meta){
        foreach($form_meta["fields"] as $field){
          if($field['type'] == "address"){
             foreach($field["inputs"] as $input){
                if($input['label'] == "State / Province"){
                  $value = $_POST["input_" . str_replace('.', '_', $input["id"])];
                  $_POST["input_" . str_replace('.', '_', $input["id"])] = shortenState($value);
                }
            }
          }
        }
      }
    function shortenState($state){
        if(strlen($state) == 2){
          $newState = $state;
        }else{
          $state = ucwords(strtolower($state));
          switch($state){
            case "District of Columbia":
              $newState = "DC";
              break;
            case "Alaska":
              $newState = "AK";
              break;
            case "Alabama":
              $newState = "AL";
              break;
            case "Arkansas":
              $newState = "AR";
              break;
            case "Arizona":
              $newState = "AZ";
              break;
            case "California":
              $newState = "CA";
              break;
            case "Colorado":
              $newState = "CO";
              break;
            case "Connecticut":
              $newState = "CT";
              break;
            case "Delaware":
              $newState = "DE";
              break;
            case "Florida":
              $newState = "FL";
              break;
            case "Georgia":
              $newState = "GA";
              break;
            case "Hawaii":
              $newState = "HI";
              break;
            case "Iowa":
              $newState = "IA";
              break;
            case "Idaho":
              $newState = "ID";
              break;
            case "Illinois":
              $newState = "IL";
              break;
            case "Indiana":
              $newState = "IN";
              break;
            case "Kansas":
              $newState = "KS";
              break;
            case "Kentucky":
              $newState = "KY";
              break;
            case "Louisiana":
              $newState = "LA";
              break;
            case "Massachusetts":
              $newState = "MA";
              break;
            case "Maryland":
              $newState = "MD";
              break;
            case "Maine":
              $newState = "ME";
              break;
            case "Michigan":
              $newState = "MI";
              break;
            case "Minnesota":
              $newState = "MN";
              break;
            case "Missouri":
              $newState = "MO";
              break;
            case "Mississippi":
              $newState = "MS";
              break;
            case "Montana":
              $newState = "MT";
              break;
            case "North Carolina":
              $newState = "NC";
              break;
            case "North Dakota":
              $newState = "ND";
              break;
            case "Nebraska":
              $newState = "NE";
              break;
            case "New Hampshire":
              $newState = "NH";
              break;
            case "New Jersey":
              $newState = "NJ";
              break;
            case "New Mexico":
              $newState = "NM";
              break;
            case "Nevada":
              $newState = "NV";
              break;
            case "New York":
              $newState = "NY";
              break;
            case "Ohio":
              $newState = "OH";
              break;
            case "Oklahoma":
              $newState = "OK";
              break;
            case "Oregon":
              $newState = "OR";
              break;
            case "Pennsylvania":
              $newState = "PA";
              break;
            case "Rhode Island":
              $newState = "RI";
              break;
            case "South Carolina":
              $newState = "SC";
              break;
            case "South Dakota":
              $newState = "SD";
              break;
            case "Tennessee":
              $newState = "TN";
              break;
            case "Texas":
              $newState = "TX";
              break;
            case "Utah":
              $newState = "UT";
              break;
            case "Virginia":
              $newState = "VA";
              break;
            case "Vermont":
              $newState = "VT";
              break;
            case "Washington":
              $newState = "WA";
              break;
            case "Wisconsin":
              $newState = "WI";
              break;
            case "West Virginia":
              $newState = "WV";
              break;
            case "Wyoming":
              $newState = "WY";
              break;
          }
        }
        return $newState;
      }
    ?>
    Posted 14 years ago on Tuesday March 30, 2010 | Permalink
  10. That plugin code works perfect, thanks. Looking forward to this functionality being incorporated into the core.

    Posted 13 years ago on Monday March 21, 2011 | Permalink
  11. Has this functionality been added to the plugin yet?

    Posted 13 years ago on Friday October 21, 2011 | Permalink
  12. This has not been added to the plugin in the latest version 1.6RC5.

    Posted 13 years ago on Friday October 21, 2011 | Permalink
  13. I would be interested in having the state name converted to postal codes in the interface itself. I think most people know the postal codes by now, and it would certainly save room in the form where you could place city, state, and zip on the same line.

    Posted 13 years ago on Sunday December 25, 2011 | Permalink