Last updated on October 18, 2023

WP Forms Excel export for entries

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

Export form entries to Excel in WP Forms.

To export WP Forms entries to an Excel file, you can use the wpsnippets_export_wpforms_entries_to_excel() function provided below. This function takes the form ID as a parameter and generates an Excel file containing all the entries for that form.

/**
 * Export WP Forms entries to Excel.
 *
 * @param int $form_id The ID of the WP Forms form.
 */
function wpsnippets_export_wpforms_entries_to_excel( $form_id ) {
    // Load the WP Forms entry class.
    if ( ! class_exists( 'WPForms_Entry' ) ) {
        require_once WP_PLUGIN_DIR . '/wpforms-lite/wpforms.php';
    }

    // Get all entries for the form.
    $entries = WPForms_Entry::get_instance()->get( array( 'form_id' => $form_id ) );

    // Create a new Excel file.
    $spreadsheet = new PhpOfficePhpSpreadsheetSpreadsheet();
    $sheet = $spreadsheet->getActiveSheet();

    // Set the column headers.
    $headers = array( 'Entry ID', 'Date', 'IP Address', 'User Agent' );
    $column = 'A';
    foreach ( $headers as $header ) {
        $sheet->setCellValue( $column . '1', $header );
        $column++;
    }

    // Set the entry data.
    $row = 2;
    foreach ( $entries as $entry ) {
        $sheet->setCellValue( 'A' . $row, $entry->entry_id );
        $sheet->setCellValue( 'B' . $row, $entry->date );
        $sheet->setCellValue( 'C' . $row, $entry->ip );
        $sheet->setCellValue( 'D' . $row, $entry->user_agent );
        $row++;
    }

    // Save the Excel file.
    $writer = new PhpOfficePhpSpreadsheetWriterXlsx( $spreadsheet );
    $writer->save( WP_CONTENT_DIR . '/uploads/wpforms-entries.xlsx' );
}

To use this function, simply call wpsnippets_export_wpforms_entries_to_excel( $form_id ) and pass the ID of the WP Forms form you want to export entries from. The function will generate an Excel file named “wpforms-entries.xlsx” in the “uploads” directory of your WordPress installation, containing the entries for the specified form.

Please note that this code assumes you have the WP Forms plugin installed and activated. Additionally, it requires the PhpSpreadsheet library to be installed. You can install it using Composer or by manually including the necessary files in your project.

Examples

Example 1: Export WP Forms entries to Excel using PHPExcel library

This example demonstrates how to export WP Forms entries to an Excel file using the PHPExcel library. The code retrieves the form entries, creates a new Excel file, and populates it with the entry data.

/**
 * Export WP Forms entries to Excel using PHPExcel library.
 */
function wpsnippets_export_wpforms_entries_to_excel() {
    // Load PHPExcel library
    require_once 'PHPExcel.php';

    // Get WP Forms entries
    $entries = wpforms()->entry->get( array( 'form_id' => 123 ) );

    // Create a new Excel file
    $excel = new PHPExcel();

    // Set the active sheet
    $sheet = $excel->getActiveSheet();

    // Set column headers
    $headers = array( 'Name', 'Email', 'Message' );
    $sheet->fromArray( $headers, null, 'A1' );

    // Populate the Excel file with entry data
    $row = 2;
    foreach ( $entries as $entry ) {
        $data = array(
            $entry->get_meta( 'name' ),
            $entry->get_meta( 'email' ),
            $entry->get_meta( 'message' ),
        );
        $sheet->fromArray( $data, null, 'A' . $row );
        $row++;
    }

    // Save the Excel file
    $writer = PHPExcel_IOFactory::createWriter( $excel, 'Excel2007' );
    $writer->save( 'wpforms_entries.xlsx' );
}

In this example, we use the PHPExcel library to export WP Forms entries to an Excel file. We first load the PHPExcel library, retrieve the form entries using the wpforms()->entry->get() function, and create a new Excel file. We then set the active sheet, add column headers, and populate the Excel file with entry data. Finally, we save the Excel file using the

Excel_IOFactory::createWriter()
function.

Example 2: Export WP Forms entries to Excel using PHPExcel library with custom fields

This example demonstrates how to export WP Forms entries to an Excel file using the PHPExcel library, including custom fields. The code retrieves the form entries, retrieves the custom field values, and populates the Excel file with the entry data and custom field values.

/**
 * Export WP Forms entries to Excel using PHPExcel library with custom fields.
 */
function wpsnippets_export_wpforms_entries_to_excel_with_custom_fields() {
    // Load PHPExcel library
    require_once 'PHPExcel.php';

    // Get WP Forms entries
    $entries = wpforms()->entry->get( array( 'form_id' => 123 ) );

    // Create a new Excel file
    $excel = new PHPExcel();

    // Set the active sheet
    $sheet = $excel->getActiveSheet();

    // Set column headers
    $headers = array( 'Name', 'Email', 'Message', 'Custom Field' );
    $sheet->fromArray( $headers, null, 'A1' );

    // Populate the Excel file with entry data and custom field values
    $row = 2;
    foreach ( $entries as $entry ) {
        $data = array(
            $entry->get_meta( 'name' ),
            $entry->get_meta( 'email' ),
            $entry->get_meta( 'message' ),
            get_post_meta( $entry->get_id(), 'custom_field', true ),
        );
        $sheet->fromArray( $data, null, 'A' . $row );
        $row++;
    }

    // Save the Excel file
    $writer = PHPExcel_IOFactory::createWriter( $excel, 'Excel2007' );
    $writer->save( 'wpforms_entries.xlsx' );
}

In this example, we extend the previous example to include custom fields in the exported Excel file. We retrieve the custom field values using the get_post_meta() function and add them to the entry data array. The rest of the code remains the same as in the previous example.

Example 3: Export WP Forms entries to Excel using WP All Export plugin

This example demonstrates how to export WP Forms entries to an Excel file using the WP All Export plugin. The code registers a custom export template for WP Forms entries and configures the export settings.

/**
 * Export WP Forms entries to Excel using WP All Export plugin.
 */
function wpsnippets_export_wpforms_entries_to_excel_with_wp_all_export() {
    // Register custom export template for WP Forms entries
    add_filter( 'wp_all_export_csv_export_templates', function( $templates ) {
        $templates['wpforms_entries'] = array(
            'label' => 'WP Forms Entries',
            'export_type' => 'csv',
            'export_format' => 'csv',
            'export_fields' => array(
                array(
                    'name' => 'Name',
                    'selector' => 'name',
                ),
                array(
                    'name' => 'Email',
                    'selector' => 'email',
                ),
                array(
                    'name' => 'Message',
                    'selector' => 'message',
                ),
            ),
        );
        return $templates;
    } );

    // Configure export settings
    add_filter( 'wp_all_export_csv_export_template_wpforms_entries', function( $template ) {
        $template['export']['delimiter'] = ',';
        $template['export']['enclosure'] = '"';
        return $template;
    } );
}

In this example, we use the WP All Export plugin to export WP Forms entries to an Excel file. We register a custom export template for WP Forms entries using the wp_all_export_csv_export_templates filter. We configure the export fields and format in the template settings. We also configure the export settings, such as the delimiter and enclosure, using the wp_all_export_csv_export_template_wpforms_entries filter.

Last updated on October 18, 2023. Originally posted on November 16, 2023.

Leave a Reply

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