Using Gravity Forms ‘gform_export_lines’ PHP action

The gform_export_lines filter allows you to modify the CSV entry export lines before they are saved to a temporary file.

Usage

add_filter('gform_export_lines', 'your_function_name');

Parameters

  • $lines (string): The lines to be included in the CSV export.

More information

See Gravity Forms Docs: gform_export_lines

Examples

Fix issue on Excel for Mac

This example fixes an encoding issue for CSV exports when opened in Excel for Mac.

add_filter('gform_export_lines', 'fix_csv_entry_export');

function fix_csv_entry_export($lines) {
    return mb_convert_encoding($lines, 'UTF-16LE', 'UTF-8');
}

Add custom header to export

This example adds a custom header to the CSV export.

add_filter('gform_export_lines', 'add_custom_header');

function add_custom_header($lines) {
    $custom_header = "Custom Header\n";
    return $custom_header . $lines;
}

Remove specific columns from export

This example removes specific columns from the CSV export.

add_filter('gform_export_lines', 'remove_specific_columns');

function remove_specific_columns($lines) {
    $lines_array = explode("\n", $lines);
    $new_lines = [];

    foreach ($lines_array as $line) {
        $columns = str_getcsv($line);
        unset($columns[2], $columns[4]); // Remove columns 3 and 5
        $new_lines[] = implode(',', $columns);
    }

    return implode("\n", $new_lines);
}

Replace comma delimiter with a tab delimiter

This example replaces the comma delimiter with a tab delimiter in the CSV export.

add_filter('gform_export_lines', 'replace_comma_with_tab');

function replace_comma_with_tab($lines) {
    return str_replace(',', "\t", $lines);
}

Remove empty lines from export

This example removes empty lines from the CSV export.

add_filter('gform_export_lines', 'remove_empty_lines');

function remove_empty_lines($lines) {
    return preg_replace('/^\n+|^[\t\s]*\n+/m', '', $lines);
}