Add your plugin’s data to the WordPress privacy export and erase tools
Since WordPress 4.9.6 (May 2018) the admin has two GDPR tools: Tools → Export Personal Data creates a ZIP with everything the site stores about an email address, and Tools → Erase Personal Data deletes or anonymises it. Core covers user profiles, comments and media. Data that a plugin keeps in its own tables is included only if the plugin registers an exporter and an eraser.
Problem: A plugin stores form submissions (name, email, message) in its own table, and they are missing from the export file and stay in the database after an erasure request.
Solution: Register an exporter with the wp_privacy_personal_data_exporters filter and an eraser with wp_privacy_personal_data_erasers. Both callbacks receive the email address and a page number, process one batch and report whether they are done.
Registering
<?php
add_filter( 'wp_privacy_personal_data_exporters', function ( $exporters ) {
$exporters['ha-form-submissions'] = array(
'exporter_friendly_name' => __( 'Form submissions', 'textdomain' ),
'callback' => 'ha_export_form_submissions',
);
return $exporters;
} );
add_filter( 'wp_privacy_personal_data_erasers', function ( $erasers ) {
$erasers['ha-form-submissions'] = array(
'eraser_friendly_name' => __( 'Form submissions', 'textdomain' ),
'callback' => 'ha_erase_form_submissions',
);
return $erasers;
} );
The exporter
<?php
function ha_export_form_submissions( $email_address, $page = 1 ) {
global $wpdb;
$per_page = 100;
$table = $wpdb->prefix . 'ha_submissions';
$rows = $wpdb->get_results( $wpdb->prepare(
"SELECT id, name, email, message, created_at FROM {$table} WHERE email = %s ORDER BY id LIMIT %d OFFSET %d",
$email_address,
$per_page,
( $page - 1 ) * $per_page
) );
$items = array();
foreach ( $rows as $row ) {
$items[] = array(
'group_id' => 'ha-form-submissions',
'group_label' => __( 'Form submissions', 'textdomain' ),
'item_id' => 'ha-submission-' . $row->id,
'data' => array(
array( 'name' => __( 'Date', 'textdomain' ), 'value' => $row->created_at ),
array( 'name' => __( 'Name', 'textdomain' ), 'value' => $row->name ),
array( 'name' => __( 'Email', 'textdomain' ), 'value' => $row->email ),
array( 'name' => __( 'Message', 'textdomain' ), 'value' => $row->message ),
),
);
}
return array(
'data' => $items,
'done' => count( $rows ) < $per_page, // false = WordPress calls again with $page + 1
);
}
Each request runs one batch over AJAX, so keep batches small enough to finish in a few seconds.
The eraser
<?php
function ha_erase_form_submissions( $email_address, $page = 1 ) {
global $wpdb;
$table = $wpdb->prefix . 'ha_submissions';
$deleted = $wpdb->query( $wpdb->prepare(
"DELETE FROM {$table} WHERE email = %s LIMIT 500",
$email_address
) );
return array(
'items_removed' => (int) $deleted,
'items_retained' => false,
'messages' => array(),
'done' => $deleted < 500,
);
}
The eraser always deletes from the start, so it does not need the page number for the offset; it simply stops when fewer rows than the batch size were left. If some data must be kept by law (invoices, for example), anonymise or keep it, set items_retained to true and explain why in messages; the text appears in the erasure report.
NOTE: Both tools work by email address, so the plugin must store the email with every record it wants to export or erase. Data that is linked only to an IP address or to a user ID needs a lookup from the email first (get_user_by( 'email', $email_address )). Also describe the stored data in the privacy policy text suggested by the plugin, with wp_add_privacy_policy_content().