Manipulating Gravity Forms exports

Gravity Forms is my go to form plugin for WordPress. Recently a client asked to change the address state field to use state codes (example: PA) rather then full states (example: Pennsylvania). While I could replace the default address field with a custom dropdown field, I didn’t want to break any past entries.

Modifing the entries on the export only

On Gravity Forms documentation I found this filter gform_leads_before_export which allows you to manipulate data right before it exports. This was perfect. It allows the entries on the website to be stored in whichever is the default format while tweaking the export format for whichever system it’s going into.

Code example taken from functions.php

add_filter( 'gform_leads_before_export_3', 'use_state_code_for_export', 10, 3 );
function use_state_code_for_export( $entries, $form, $paging ) {

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

    foreach( $entries as &$entry ) {
        $current_state = $entry['6.4'];
        $state_code = array_search($current_state, $states);

        $entry['6.4'] = $state_code;
    }

    return $entries;
}