Last updated on October 18, 2023

WP Forms data export to CSV

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Export form data to CSV files in WP Forms.

To export WP Forms data to a CSV file, you can use the wpsnippets_export_wpforms_data_to_csv() function provided below. This function takes two parameters: the form ID and the file path where the CSV file will be saved.

function wpsnippets_export_wpforms_data_to_csv($form_id, $file_path) {
    global $wpdb;

    $entries = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}wpforms_entries WHERE form_id = %d",
            $form_id
        )
    );

    if (empty($entries)) {
        return;
    }

    $csv_data = array();

    foreach ($entries as $entry) {
        $fields = json_decode($entry->fields, true);

        $row = array();
        foreach ($fields as $field) {
            $row[] = $field['value'];
        }

        $csv_data[] = $row;
    }

    $file = fopen($file_path, 'w');

    foreach ($csv_data as $row) {
        fputcsv($file, $row);
    }

    fclose($file);
}

To use this function, you need to provide the form ID and the file path where the CSV file will be saved. For example:

wpsnippets_export_wpforms_data_to_csv(123, '/path/to/exported_data.csv');

This code snippet retrieves all entries for a specific WP Forms form using the $wpdb global object. It then loops through each entry, extracts the field values, and adds them to a CSV data array. Finally, it writes the CSV data to a file using the fputcsv() function.

Examples

Example 1: Export WP Forms data to CSV using a custom function

This example demonstrates how to export WP Forms data to a CSV file using a custom PHP function. The function wpsnippets_export_wpforms_data_to_csv() takes the form ID as a parameter and exports the form entries to a CSV file.

function wpsnippets_export_wpforms_data_to_csv( $form_id ) {
    $entries = wpforms()->entry->get( array( 'form_id' => $form_id ) );

    if ( empty( $entries ) ) {
        return;
    }

    $csv_data = array();

    foreach ( $entries as $entry ) {
        $csv_data[] = array(
            'Name'    => $entry->get_meta( 'name' ),
            'Email'   => $entry->get_meta( 'email' ),
            'Message' => $entry->get_meta( 'message' ),
        );
    }

    $csv_file = fopen( 'wpforms-export.csv', 'w' );

    if ( $csv_file ) {
        fputcsv( $csv_file, array_keys( $csv_data[0] ) );

        foreach ( $csv_data as $data ) {
            fputcsv( $csv_file, $data );
        }

        fclose( $csv_file );
    }
}

This code retrieves the form entries using the wpforms()->entry->get() method and loops through each entry to extract the desired data. It then creates a CSV file using fopen() and writes the data using fputcsv().

Example 2: Export WP Forms data to CSV using a plugin

This example demonstrates how to export WP Forms data to a CSV file using a plugin called “WPForms Entries Export”. The plugin provides a user-friendly interface to export form entries to CSV.

/**
 * Plugin Name: WPForms Entries Export
 * Description: Export WPForms entries to CSV.
 * Version: 1.0
 * Author: Your Name
 */

function wpsnippets_wpforms_entries_export() {
    add_submenu_page(
        'wpforms-overview',
        'Export Entries',
        'Export Entries',
        'manage_options',
        'wpforms-entries-export',
        'wpsnippets_wpforms_entries_export_page'
    );
}
add_action( 'admin_menu', 'wpsnippets_wpforms_entries_export' );

function wpsnippets_wpforms_entries_export_page() {
    // Export form entries to CSV code here
}

This code creates a custom plugin called “WPForms Entries Export” that adds a submenu page under the WPForms menu. The function wpsnippets_wpforms_entries_export_page() is responsible for handling the export functionality, which can be implemented within this function.

Example 3: Export WP Forms data to CSV using a third-party plugin

This example demonstrates how to export WP Forms data to a CSV file using a third-party plugin called “WP All Export”. The plugin provides advanced features for exporting various types of data, including WP Forms entries.

/**
 * Plugin Name: WP All Export WPForms Add-On
 * Description: Export WPForms entries using WP All Export.
 * Version: 1.0
 * Author: Your Name
 * Requires at least: 5.0
 * Tested up to: 5.8
 */

function wpsnippets_wp_all_export_wpforms_addon() {
    if ( class_exists( 'WPForms' ) && class_exists( 'AllExport' ) ) {
        add_action( 'plugins_loaded', 'wpsnippets_wp_all_export_wpforms_addon_init' );
    }
}
add_action( 'plugins_loaded', 'wpsnippets_wp_all_export_wpforms_addon' );

function wpsnippets_wp_all_export_wpforms_addon_init() {
    // WP All Export integration code here
}

This code creates a custom plugin called “WP All Export WPForms Add-On” that integrates WPForms with the WP All Export plugin. The integration is triggered when both WPForms and WP All Export plugins are active. The export functionality can be implemented within the wpsnippets_wp_all_export_wpforms_addon_init() function.

Last updated on October 18, 2023. Originally posted on December 7, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *