Skip to content

Instantly share code, notes, and snippets.

@JRyven
Last active October 25, 2020 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JRyven/243b9819c23cb26480fbe755a779e5b2 to your computer and use it in GitHub Desktop.
Save JRyven/243b9819c23cb26480fbe755a779e5b2 to your computer and use it in GitHub Desktop.
<?php
function populate_csv( $entry, $form ) {
/* remove some questions from the output via label filtering */
$exclusions = array(
'Field Name One',
'Field Name Two',
'Field Name Three',
);
/* datetime */
date_default_timezone_set('America/New_York');
$date = date('U', time());
/* user data */
$user = get_current_user_id();
/* file data */
$headers = "Label\tData".PHP_EOL;
$filename = $user.'-'.$date.'.txt';
$field_and_response = '';
/* loop through the fields */
foreach($form["fields"] as &$field) {
/* see if this is a multi-field, like name or address */
if (is_array($field["inputs"])) {
/* loop through inputs */
foreach($field["inputs"] as &$input) {
/* get value from entry object; change the id to a string */
$value = $entry[strval($input["id"])];
/* get label; if label is not in the array of excludes */
$label = $input["label"];
if (!in_array($label, $exclusions)) {
/* ...add field and response */
$field_and_response .= $label."\t".$value.PHP_EOL;
}
}
} else {
/* get value from entry object; change the id to a string */
$value = $entry[strval($field["id"])];
/* get label; if label is not in the array of excludes */
$label = $field["label"];
if (!in_array($label, $exclusions)) {
/* ...add field and response */
$field_and_response .= $label."\t".$value.PHP_EOL;
}
}
}
/* delete existing file */
if( file_exists($_SERVER["DOCUMENT_ROOT"].'/wp-content/uploads/folder/'.$filename) ){
unlink( $_SERVER["DOCUMENT_ROOT"].'/wp-content/uploads/folder/'.$filename );
}
/* create file */
$fh = fopen( $_SERVER["DOCUMENT_ROOT"].'/wp-content/uploads/folder/'.$filename, 'a');
/* add headers */
fwrite($fh, $headers);
/* add data */
fwrite( $fh, $field_and_response );
/* Close the file */
fclose($fh);
}
add_action( 'gform_after_submission', 'populate_csv', 10, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment