Last updated on October 18, 2023

WP Forms form import/export

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

Import/export forms and data in WP Forms.

The code snippet below demonstrates how to import and export WP Forms forms using the built-in WordPress functions. This functionality can be useful when you want to migrate forms from one WordPress site to another or when you want to create a backup of your forms.

To export a WP Forms form, you can use the wpsnippets_export_wpforms_form() function. This function takes the form ID as a parameter and returns the exported form as a JSON string.

function wpsnippets_export_wpforms_form($form_id) {
    $form = wpforms()->form->get($form_id);
    return wp_json_encode($form);
}

To import a WP Forms form, you can use the wpsnippets_import_wpforms_form() function. This function takes the exported form JSON string as a parameter and creates a new form using the WP Forms API.

function wpsnippets_import_wpforms_form($form_json) {
    $form_data = json_decode($form_json, true);
    $form_id = wpforms()->form->add($form_data);
    return $form_id;
}

Here’s an example of how you can use these functions to export and import a WP Forms form:

// Export a form
$form_id = 123;
$form_json = wpsnippets_export_wpforms_form($form_id);

// Import the form
$new_form_id = wpsnippets_import_wpforms_form($form_json);

In the example above, we first export a WP Forms form with the ID 123 using the wpsnippets_export_wpforms_form() function. The exported form is then stored in the $form_json variable as a JSON string.

Next, we import the form using the wpsnippets_import_wpforms_form() function, passing the $form_json variable as the parameter. The function creates a new form using the WP Forms API and returns the ID of the newly created form, which is stored in the $new_form_id variable.

Examples

Example 1: Export WP Forms form data to a CSV file

This use case demonstrates how to export WP Forms form data to a CSV file. The code example below shows how to retrieve form entries and export them to a CSV file using the wpsnippets_export_wpforms_entries_to_csv() function.

/**
 * Export WP Forms form entries to a CSV file.
 *
 * @param int $form_id The ID of the WP Forms form.
 * @param string $file_path The path to the CSV file.
 */
function wpsnippets_export_wpforms_entries_to_csv( $form_id, $file_path ) {
    $entries = wpforms()->entry->get( array( 'form_id' => $form_id ) );

    if ( ! empty( $entries ) ) {
        $csv_data = array();

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

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

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

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

            fclose( $csv_file );
        }
    }
}

The wpsnippets_export_wpforms_entries_to_csv() function accepts the form ID and the file path as parameters. It retrieves the form entries using the wpforms()->entry->get() method and then loops through each entry to extract the desired data. The data is then written to a CSV file using the fputcsv() function.

Example 2: Import WP Forms form data from a CSV file

This use case demonstrates how to import WP Forms form data from a CSV file. The code example below shows how to read a CSV file and import its data into WP Forms using the wpsnippets_import_wpforms_entries_from_csv() function.

/**
 * Import WP Forms form entries from a CSV file.
 *
 * @param int $form_id The ID of the WP Forms form.
 * @param string $file_path The path to the CSV file.
 */
function wpsnippets_import_wpforms_entries_from_csv( $form_id, $file_path ) {
    if ( ( $handle = fopen( $file_path, 'r' ) ) !== false ) {
        $headers = fgetcsv( $handle );

        while ( ( $data = fgetcsv( $handle ) ) !== false ) {
            $entry = array();

            foreach ( $headers as $index => $header ) {
                $entry[ $header ] = $data[ $index ];
            }

            wpforms()->entry->add( $form_id, $entry );
        }

        fclose( $handle );
    }
}

The wpsnippets_import_wpforms_entries_from_csv() function accepts the form ID and the file path as parameters. It reads the CSV file using the fopen() function and retrieves the headers from the first row using fgetcsv(). It then loops through each row of data, maps the headers to the corresponding values, and adds the entry to WP Forms using the wpforms()->entry->add() method.

Example 3: Export and import WP Forms form settings

This use case demonstrates how to export and import WP Forms form settings. The code example below shows how to export form settings to a JSON file and import them back using the wpsnippets_export_wpforms_settings_to_json() and wpsnippets_import_wpforms_settings_from_json() functions.

/**
 * Export WP Forms form settings to a JSON file.
 *
 * @param int $form_id The ID of the WP Forms form.
 * @param string $file_path The path to the JSON file.
 */
function wpsnippets_export_wpforms_settings_to_json( $form_id, $file_path ) {
    $form_data = wpforms()->form->get( $form_id );

    if ( ! empty( $form_data ) ) {
        $json_data = json_encode( $form_data );

        file_put_contents( $file_path, $json_data );
    }
}

/**
 * Import WP Forms form settings from a JSON file.
 *
 * @param int $form_id The ID of the WP Forms form.
 * @param string $file_path The path to the JSON file.
 */
function wpsnippets_import_wpforms_settings_from_json( $form_id, $file_path ) {
    if ( file_exists( $file_path ) ) {
        $json_data = file_get_contents( $file_path );

        if ( $json_data ) {
            $form_data = json_decode( $json_data, true );

            if ( ! empty( $form_data ) ) {
                wpforms()->form->update( $form_id, $form_data );
            }
        }
    }
}

The wpsnippets_export_wpforms_settings_to_json() function accepts the form ID and the file path as parameters. It retrieves the form settings using the wpforms()->form->get() method, encodes them to JSON using

on_encode()
, and saves the JSON data to a file using file_put_contents().

The wpsnippets_import_wpforms_settings_from_json() function accepts the form ID and the file path as parameters. It reads the JSON data from the file using file_get_contents(), decodes it using

on_decode()
, and updates the form settings using wpforms()->form->update().

Last updated on October 18, 2023. Originally posted on January 20, 2024.

Leave a Reply

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