Last updated on October 18, 2023

WP Forms GDPR compliance

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

Ensure WP Forms complies with GDPR.

To achieve GDPR compliance with WP Forms, you can use the wpsnippets_wpforms_gdpr_consent_field function to add a consent field to your form. This function creates a checkbox field that users must check to give their consent before submitting the form.

Here’s an example of how to use this function:

function wpsnippets_wpforms_gdpr_consent_field( $fields, $form_id ) {
    // Add a consent field to the form
    $fields['gdpr_consent'] = array(
        'label'       => 'I consent to the storage of my data',
        'type'        => 'checkbox',
        'required'    => true,
        'description' => 'By checking this box, you agree to our privacy policy.',
    );

    return $fields;
}
add_filter( 'wpforms_form_fields', 'wpsnippets_wpforms_gdpr_consent_field', 10, 2 );

In this example, the wpsnippets_wpforms_gdpr_consent_field function adds a consent field to the WP Forms form. The field has a label, type, and description. The required parameter is set to true, meaning users must check the checkbox before submitting the form.

To use this code snippet, you need to replace the label, type, and description with your own text. You can also modify the required parameter to suit your needs.

Examples

Example 1: Adding a GDPR consent checkbox to a WP Forms form

This example demonstrates how to add a GDPR consent checkbox to a WP Forms form. The checkbox will be required for the user to submit the form.

function wpsnippets_add_gdpr_consent_checkbox( $fields ) {
    $fields['gdpr_consent'] = array(
        'label'       => 'I consent to the storage of my data',
        'type'        => 'checkbox',
        'required'    => true,
        'description' => 'Please check this box to give your consent.',
    );

    return $fields;
}
add_filter( 'wpforms_fields', 'wpsnippets_add_gdpr_consent_checkbox' );

In this code example, we use the wpforms_fields filter to add a new field to the WP Forms form. The field is a checkbox with a label, description, and required attribute. By returning the modified $fields array, we add the GDPR consent checkbox to the form.

Example 2: Storing GDPR consent data in a custom database table

This example demonstrates how to store the GDPR consent data submitted through a WP Forms form in a custom database table.

function wpsnippets_store_gdpr_consent_data( $entry, $form_data ) {
    global $wpdb;

    $table_name = $wpdb->prefix . 'gdpr_consent_data';

    $data = array(
        'form_id'    => $form_data['id'],
        'entry_id'   => $entry['id'],
        'user_email' => $entry['fields'][0]['value'],
        'consent'    => $entry['fields'][1]['value'],
        'timestamp'  => current_time( 'mysql' ),
    );

    $wpdb->insert( $table_name, $data );
}
add_action( 'wpforms_process_entry_save', 'wpsnippets_store_gdpr_consent_data', 10, 2 );

In this code example, we use the wpforms_process_entry_save action to store the GDPR consent data in a custom database table. We retrieve the form ID, entry ID, user email, consent value, and current timestamp from the submitted form data. Then, we insert this data into the custom table using the $wpdb global object.

Example 3: Displaying GDPR consent data in the WP Forms entry details

This example demonstrates how to display the GDPR consent data in the WP Forms entry details.

function wpsnippets_display_gdpr_consent_data( $fields, $entry ) {
    $fields['gdpr_consent'] = array(
        'label' => 'GDPR Consent',
        'value' => $entry['fields'][1]['value'],
    );

    return $fields;
}
add_filter( 'wpforms_entry_detail_fields', 'wpsnippets_display_gdpr_consent_data', 10, 2 );

In this code example, we use the wpforms_entry_detail_fields filter to add the GDPR consent data to the WP Forms entry details. We retrieve the consent value from the entry data and add it as a new field with a label and value. By returning the modified $fields array, we display the GDPR consent data in the entry details.

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

Leave a Reply