How to customize the Excel export file name
The Excel button on a DataTables View names the file after the page’s title, which on most sites is the page name followed by the site name. The same title is also the first row of the spreadsheet. A short PHP snippet sets your own file name and removes that title row.
The View needs the Excel button turned on. In the View’s Settings box, click the DataTables tab, check Enable Buttons, and check Excel. Buttons (DataTables Setting) shows the setting.
Set the file name #
Replace staff-directory with the name you want. Leave out the extension: DataTables adds .xlsx.
/**
* Sets the file name of the DataTables Excel export and removes its title row.
*
* @param array $button_config The Excel button's options.
* @param int $view_id The ID of the View.
*
* @return array The changed options.
*/
add_filter( 'gravityview/datatables/button_excel', function ( $button_config, $view_id ) {
$button_config['filename'] = 'staff-directory';
$button_config['title'] = '';
return $button_config;
}, 10, 2 );Note: The PHP examples on this page go in your site’s code. See Where to put code samples.
The title line removes the title row, so the spreadsheet starts with the column headings. To keep a title row with your own words, put them between the quotes. To keep the page’s title, delete the line.
Change the name for one View #
To set the name on one View only, compare the View ID first:
add_filter( 'gravityview/datatables/button_excel', function ( $button_config, $view_id ) {
// Replace 123 with the ID of your View.
if ( 123 !== (int) $view_id ) {
return $button_config;
}
$button_config['filename'] = 'staff-directory';
return $button_config;
}, 10, 2 );Other export buttons #
Each export button has its own filter with the same two arguments, such as gravityview/datatables/button_csv, gravityview/datatables/button_pdf and gravityview/datatables/button_print. To change every button at once, use gravityview/datatables/button. How to change the separator in DataTables CSV exports uses the CSV filter.