"Download My Data" Request Button
Adds a [wpide_export_data] shortcode with a button that asks WordPress's privacy tools to prepare a copy of the member's personal data, confirmed by email.
Opens WPIDE on
Code
add_action( 'template_redirect', function () {
if ( empty( $_POST['wpide_export_nonce'] ) || ! is_user_logged_in() ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_POST['wpide_export_nonce'] ), 'wpide_export_request' ) ) {
wp_die( 'The form expired. Please go back and try again.' );
}
$request_id = wp_create_user_request( wp_get_current_user()->user_email, 'export_personal_data' );
if ( is_wp_error( $request_id ) ) {
$state = 'duplicate_request' === $request_id->get_error_code() ? 'pending' : 'error';
} else {
wp_send_user_request( $request_id );
$state = 'sent';
}
wp_safe_redirect( add_query_arg( 'export-request', $state ) );
exit;
} );
add_shortcode( 'wpide_export_data', function () {
if ( ! is_user_logged_in() ) {
return '';
}
$messages = array(
'sent' => 'Check your email to confirm the request. We will send your data once it is ready.',
'pending' => 'You already asked for a copy of your data. Check your email for the confirmation link.',
'error' => 'Something went wrong. Please contact us for a copy of your data.',
);
$state = isset( $_GET['export-request'] ) ? sanitize_key( $_GET['export-request'] ) : '';
if ( isset( $messages[ $state ] ) ) {
return '<p class="wpide-export-notice" role="status">' . esc_html( $messages[ $state ] ) . '</p>';
}
return '<form method="post" class="wpide-export-form">'
. wp_nonce_field( 'wpide_export_request', 'wpide_export_nonce', true, false )
. '<p><button type="submit">Request a copy of my data</button></p></form>';
} );
Notes
You send the export from Tools > Export Personal Data, or let WordPress email it once you approve the request.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.