Privacy PHP Intermediate

Include Custom User Fields in Privacy Exports and Erasures

Add your own user fields, like phone or job title, to WordPress's Export Personal Data and Erase Personal Data tools.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 56 lines
function nahnu_privacy_fields(): array {
	// Meta key => label.
	return [
		'nahnu_job_title' => 'Job title',
		'nahnu_phone'     => 'Phone',
	];
}

add_filter( 'wp_privacy_personal_data_exporters', function ( array $exporters ): array {
	$exporters['nahnu-profile'] = [
		'exporter_friendly_name' => 'Extra profile fields',
		'callback'               => function ( string $email, int $page = 1 ): array {
			$user = get_user_by( 'email', $email );
			$data = [];

			if ( $user ) {
				foreach ( nahnu_privacy_fields() as $key => $label ) {
					$value = get_user_meta( $user->ID, $key, true );
					if ( '' !== $value ) {
						$data[] = [ 'name' => $label, 'value' => $value ];
					}
				}
			}

			return [
				'data' => $data ? [ [
					'group_id'    => 'nahnu-profile',
					'group_label' => 'Extra profile fields',
					'item_id'     => 'user-' . ( $user->ID ?? 0 ),
					'data'        => $data,
				] ] : [],
				'done' => true,
			];
		},
	];
	return $exporters;
} );

add_filter( 'wp_privacy_personal_data_erasers', function ( array $erasers ): array {
	$erasers['nahnu-profile'] = [
		'eraser_friendly_name' => 'Extra profile fields',
		'callback'             => function ( string $email, int $page = 1 ): array {
			$user    = get_user_by( 'email', $email );
			$removed = false;

			if ( $user ) {
				foreach ( array_keys( nahnu_privacy_fields() ) as $key ) {
					$removed = delete_user_meta( $user->ID, $key ) || $removed;
				}
			}

			return [ 'items_removed' => $removed, 'items_retained' => false, 'messages' => [], 'done' => true ];
		},
	];
	return $erasers;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress’s privacy tools, under Tools → Export Personal Data and Erase Personal Data, only know about the data WordPress and well-behaved plugins register. If you’ve added your own profile fields, they’re left out, and your exports are incomplete. This snippet adds them.

How it works

wp_privacy_personal_data_exporters registers an exporter. Given an email address, it finds the user and returns each of your fields that has a value, grouped under “Extra profile fields” in the export file.
wp_privacy_personal_data_erasers registers an eraser that deletes those fields for the user and reports whether anything was removed.

List your meta keys and labels once, in nahnu_privacy_fields(), and both tools use them.

Things to watch

  • For data stored outside user meta, like a custom table, the callbacks need their own queries. Use $page and 'done' => false to process large amounts in batches.
  • Run a test export on your own account before you need it for a real request.

How to test it

Go to Tools → Export Personal Data, add your own email and download the export. It should include an “Extra profile fields” section.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only