Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spicydog/edf4ef4f120f329f423fa8c2a3cbbdd0 to your computer and use it in GitHub Desktop.
Save spicydog/edf4ef4f120f329f423fa8c2a3cbbdd0 to your computer and use it in GitHub Desktop.

On Caldera Forms, when you export entries as CSV.

It cannot be open perfectly with excel because it missing BOM in file prefix.

Therefore, we have to intercept the process and here is how to.

At the file wp-content/plugins/caldera-forms/classes/admin.php.

There is a function to export the CSV here.

What we need to intercept is during the process of writing the CSV file about line 1411.

Here is some chunk of the code.

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: text/csv charset=$encoding;");
header("Content-Disposition: attachment; filename=\"" . sanitize_file_name( $form['name'] ) . ".csv\";" );
header("Content-Transfer-Encoding: binary");
$df = fopen("php://output", 'w');

After the $df starts the file, we put BOM to it with this code.

// Put BOM to the file
fprintf($df, chr(0xEF).chr(0xBB).chr(0xBF));

The continue the process.

There is the complete chuck of the edited code.

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: text/csv charset=$encoding;");
header("Content-Disposition: attachment; filename=\"" . sanitize_file_name( $form['name'] ) . ".csv\";" );
header("Content-Transfer-Encoding: binary");
$df = fopen("php://output", 'w');

// Put BOM to the file
fprintf($df, chr(0xEF).chr(0xBB).chr(0xBF));

$csv_data = apply_filters( 'caldera_forms_admin_csv', array(
	'headers' => $headers,
	'data' => $data
), $form );
$data = $csv_data[ 'data' ];
$headers = $csv_data[ 'headers' ];
fputcsv($df, $headers);

The file maybe replaced when the plugin is updated, so make sure you stop auto update by locking the file with chmod to 444 and do manual update instead and reprocess this again after plugin update.

@SzaZo74
Copy link

SzaZo74 commented May 9, 2018

Try this in function.php, it might help:

add_filter( 'caldera_forms_admin_csv', function( $csv_data, $form ){
//replace with your form id
if( 'CF5a57806b565d2' === $form[ 'ID' ] ){
$df = fopen("php://output", 'w');
fprintf($df, chr(0xEF).chr(0xBB).chr(0xBF));
}
return $csv_data;
}, 10, 2 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment