How to customize the CSV field separator?

When exporting entries from a DataTables View, the default field separator is a comma (, ). You can change this to a different character using the gravityview_datatables_js_options  filter.

When to change the separator

You may need a different separator when:

  • Your spreadsheet software expects a different format
  • You need to import the data into a system that requires a specific delimiter
  • You want to create a TSV (tab-separated values) file instead of CSV

Common separators

Separator Character Use case
Comma , Default CSV format
Tab \t TSV files
Semicolon ; Common in European locales

Example: Change to tab-separated (TSV)

Add this code to your site using one of the methods described in Adding custom PHP code snippets to your website:

/**
 * Changes the CSV export separator to a tab character for TSV format.
 *
 * @param array $config The DataTables configuration array.
 * @param int $view_id The ID of the current View.
 * @return array Modified configuration array.
 */
add_filter( 'gravityview_datatables_js_options', function( $config, $view_id ) {
    if ( empty( $config['tableTools']['aButtons'] ) ) {
        return $config;
    }

    foreach ( $config['tableTools']['aButtons'] as &$button ) {
        if ( 'csv' === $button['sExtends'] ) {
            $button['sFieldSeperator'] = "\t"; // Tab character for TSV
        }
    }

    return $config;
}, 20, 2 );

Example: Change to semicolon separator

/**
 * Changes the CSV export separator to a semicolon.
 *
 * @param array $config The DataTables configuration array.
 * @param int $view_id The ID of the current View.
 * @return array Modified configuration array.
 */
add_filter( 'gravityview_datatables_js_options', function( $config, $view_id ) {
    if ( empty( $config['tableTools']['aButtons'] ) ) {
        return $config;
    }

    foreach ( $config['tableTools']['aButtons'] as &$button ) {
        if ( 'csv' === $button['sExtends'] ) {
            $button['sFieldSeperator'] = ';';
        }
    }

    return $config;
}, 20, 2 );

Example: Change separator for a specific View

/**
 * Changes the CSV export separator to a tab, but only for View ID 123.
 *
 * @param array $config The DataTables configuration array.
 * @param int $view_id The ID of the current View.
 * @return array Modified configuration array.
 */
add_filter( 'gravityview_datatables_js_options', function( $config, $view_id ) {
    // Only apply to View ID 123
    if ( 123 !== $view_id ) {
        return $config;
    }

    if ( empty( $config['tableTools']['aButtons'] ) ) {
        return $config;
    }

    foreach ( $config['tableTools']['aButtons'] as &$button ) {
        if ( 'csv' === $button['sExtends'] ) {
            $button['sFieldSeperator'] = "\t";
        }
    }

    return $config;
}, 20, 2 );
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us