Can I change the CSV column headers for an e-mail CSV attachment?

By: Floris Baan | Asked: 12/02/2022
ForumsCategory: Code HelpCan I change the CSV column headers for an e-mail CSV attachment?
Floris Baan asked 2 years ago
I have an e-mail action set up that also attaches the CSV file of the entry on submit. I would like to change the headers inside this CSV attachment. I thought I could do this using the 'frm_map_csv_field' hook. But it seems this hook only works for exports through the entries view. Which to me seems strange? Is it possible to change the column headers for the CSV e-mail attachment?
Question Tags:
2 Answers
Best Answer
Floris Baan answered 2 years ago

While browsing the plug-in code I've found the filter 'frm_fields_for_csv_export' (https://formidableforms.com/knowledgebase/frm_fields_for_csv_export/) which actually is being used before attaching the CSV to an email.  This is the code I eventually used to make it work.

add_filter( 'frm_fields_for_csv_export', 'sv_change_headers_for_csv_export', 10, 2 ); 
function sv_change_headers_for_csv_export( $fields, $args ) {
$target_form_id = 3; // change 220 to the ID of the form
if ( $target_form_id === (int) $args['form']->id ) {
// change 67 to your field ids, and the text to the new CSV headers
$matches = array(
67 => 'New name'
);
// Loops all $fields and changes the names of the fields that are exported to CSV
foreach ( $fields as $field ) {
if ( isset($matches[$field->id] ) ) {
$field->name = $matches[$field->id];
}
}
}
return $fields;
}

Bobby Clapp replied 2 years ago

Glad you were able to resolve it and thanks for sharing the final solution.

Bobby Clapp answered 2 years ago
How about this one: https://formidableforms.com/knowledgebase/frm_export_csv_table_heading/#kb-examples
Floris Baan replied 2 years ago

I’m afraid this looks to be changing the headers of the CSV when exporting from a view (when exporting an entry from the admin). I need them changed in the email attachment right after an entry is submitted.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right