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(
        'DC'=>'District of Columbia',
        'NH'=>'New Hampshire',
        'NJ'=>'New Jersey',
        'NM'=>'New Mexico',
        'NY'=>'New York',
        'NC'=>'North Carolina',
        'ND'=>'North Dakota',
        'RI'=>'Rhode Island',
        'SC'=>'South Carolina',
        'SD'=>'South Dakota',
        'WV'=>'West Virginia',

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

        $entry['6.4'] = $state_code;

    return $entries;