Description
Use this filter to override the field value before it is included in the CSV export. This filter can be used in conjunction with the gform_export_fields filter to include custom columns in the export.
Usage
add_filter( 'gform_export_field_value', 'your_function_name', 10, 4 );
Parameters
- $value string
Value of the field being exported.
- $form_id integer
ID of the current form.
-
$field_id integer
ID of the current field.
-
$entry Entry Object
The current entry.
Examples
1. Set export value for custom fields
This example sets the values for two custom fields that were added to the export. The custom fields are named “custom_field1” and “custom_field2”.
add_filter( 'gform_export_field_value', 'set_export_values', 10, 4 );
function set_export_values( $value, $form_id, $field_id, $entry ) {
switch ( $field_id ) {
case 'custom_field1' :
$value = 'valueforcustomfield1';
break;
case 'custom_field2' :
$value = 'valueforcustomfield2';
break;
}
return $value;
}
2. Use choice text instead of values
add_filter( 'gform_export_field_value', 'export_choice_text', 10, 4 );
function export_choice_text( $value, $form_id, $field_id, $entry ) {
$field = GFAPI::get_field( $form_id, $field_id );
return is_object( $field ) && is_array( $field->choices ) ? $field->get_value_export( $entry, $field_id, true ) : $value;
}
3. Decode special characters
The following example shows how you can decode special characters such as the ampersand which may have been encoded during field value sanitization.
add_filter( 'gform_export_field_value', 'decode_export_values' );
function decode_export_values( $value ) {
return htmlspecialchars_decode( $value );
}
4. Format date field value
add_filter( 'gform_export_field_value', 'format_date', 10, 4 );
function format_date( $value, $form_id, $field_id, $entry ) {
$field = GFAPI::get_field( $form_id, $field_id );
return is_object( $field ) && $field->type == 'date' ? GFCommon::date_display( $value, $field->dateFormat ) : $value;
}
5. Use display name instead of user id
add_filter( 'gform_export_field_value', 'export_user_display_name', 10, 4 );
function export_user_display_name( $value, $form_id, $field_id, $entry ) {
if ( $field_id == 'created_by' ) {
$user = get_user_by( 'id', $value );
return is_object( $user ) ? $user->display_name : $value;
}
return $value;
}
6. Format Number field value
add_filter( 'gform_export_field_value', 'format_number_field_value', 10, 4 );
function format_number_field_value( $value, $form_id, $field_id, $entry ) {
$field = GFAPI::get_field( $form_id, $field_id );
$include_thousands_sep = true;
return is_object( $field ) && $field->type == 'number' ? GFCommon::format_number( $value, $field->numberFormat, rgar( $entry, 'currency' ), $include_thousands_sep ) : $value;
}
7. Remove new lines, tabs and carriage returns
The following example shows how you can remove new lines, tabs and carriage returns before exporting the field value. Useful when importing CSV files in Excel.
add_filter( 'gform_export_field_value', 'decode_export_values' );
function decode_export_values( $value ) {
return str_replace( array( "\n", "\t", "\r" ), '', $value );
}
8. Export the secure file download URL
This example shows how you can replace the direct file URL with the secure file download URL.
add_filter( 'gform_export_field_value', function ( $value, $form_id, $field_id ) {
if ( ! empty( $value ) ) {
$field = GFAPI::get_field( $form_id, $field_id );
if ( $field && $field->get_input_type() === 'fileupload' ) {
$urls = explode( ' , ', $value );
foreach ( $urls as &$url ) {
$url = $field->get_download_url( $url, $force_download = true );
}
$value = implode( ' , ', $urls );
}
}
return $value;
}, 10, 3 );
9. Export customized Quiz field values
This example shows how to export values for Quiz fields. This is useful only when you have set custom values after using the gform_quiz_show_choice_values filter.
// Note that priority is set to 20 to run after the Quiz add-on. This is required.
add_filter( 'gform_export_field_value', 'export_quiz_value', 20, 4 );
function export_quiz_value( $value, $form_id, $field_id, $entry ) {
$field = GFAPI::get_field( $form_id, $field_id );
if ( $field->type == 'quiz' ) {
GFCommon::log_debug( __METHOD__ . '(): Running for field id: => ' . $field_id );
$value = rgar( $entry, $field_id );
GFCommon::log_debug( __METHOD__ . '(): Value from entry: => ' . $value );
}
return $value;
}
Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in GFExport::start_export() in export.php.