Displaying Field Information Form Forms in a CSV

Est. Reading: 2 minutes
By: Walter Jones
Created: 02/22/2023
Category:
Difficulty: Intermediate

Displaying Field Information Form Forms in a CSV

Recently a question was posed in the forum on how to export information from forms into a .csv file so that that the field name, field ID and field key is displayed.  I have a need for this as well, so I here is what satisfied my particular need for a client and I wanted to share the code snippet with the community encase it would prove helpful to others.

Using the code snippets plugin or by adding to your themes function.php file, put add the following code:

<code>

<?php
/* Export Form Fields to CSV */
function export_form_fields_to_csv() {
// Check if Formidable Forms plugin is installed and activated
if ( ! class_exists( 'FrmFormsController' ) ) {
return 'Formidable Forms plugin is not activated';
}

$forms = FrmForm::getAll(); // Get all Formidable Forms

// Create an array to store the form field data
$fields_data = array();

// Loop through each form
foreach ( $forms as $form ) {
$form_fields = FrmField::get_all_for_form( $form->id );

// Loop through each form field
foreach ( $form_fields as $field ) {
$field_data = array(
'form_name' => $form->name,
'field_name' => $field->name,
'field_type' => $field->type,
'field_id' => $field->id,
'field_key' => $field->field_key,
);

// Add the field data to the array
$fields_data[] = $field_data;
}
}

// Create the CSV file
$csv_file = fopen( 'form_fields.csv', 'w' );

// Add the CSV header
fputcsv( $csv_file, array( 'Form Name', 'Field Name', 'Field Type', 'Field ID', 'Field Key' ) );

// Add the data rows
foreach ( $fields_data as $field_data ) {
fputcsv( $csv_file, $field_data );
}

// Close the CSV file
fclose( $csv_file );

// Return the file path for the download link
return get_site_url() . '/form_fields.csv';
}

// Register the shortcode for the download link
function form_fields_csv_download_link_shortcode() {
$csv_url = export_form_fields_to_csv();

// Check if there was an error during the export process
if ( is_string( $csv_url ) ) {
return $csv_url;
}

// Return the download link markup with inline CSS
$download_link = '<a style="display: inline-block; margin: 10px 0; padding: 8px 16px; font-size: 14px; font-weight: 600; line-height: 1.5; color: #007bff; background-color: transparent; border: 2px solid #007bff; border-radius: 4px; text-align: center; text-decoration: none; transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out; box-shadow: none;" href="' . $csv_url . '" download="form_fields.csv">Download Form Fields</a>';

return $download_link;
}
add_shortcode( 'form_fields_csv_download_link', 'form_fields_csv_download_link_shortcode' );

</code>

This snippet creates a short-code that you can add to a wordpress page or post that will create a link to download a CSV file which contains a list of all of your forms Field Names, Field ID's and Field Keys and Field Type.

The shortcode is:

<code>

[form_fields_csv_download_link]

</code>

Again I hope this is helpful!

2 comments on “Displaying Field Information Form Forms in a CSV”

  1. This is *great* for documenting projects and for keeping things in a more complicated setup organized and understandable. Wonderful tutorial!

    Many thanks to whomever put this together for the community. Greatly appreciated!

Leave a Reply

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
crosschevron-leftchevron-rightarrow-right