Email the Admin When Users Change Their Profile
Sends the site admin a short email listing what changed when a member updates their own name, email, website or bio.
Opens WPIDE on
Code
// Fields to watch: user field or meta key => label.
$wpide_watch_fields = array(
'user_email' => 'Email',
'display_name' => 'Display name',
'user_url' => 'Website',
'first_name' => 'First name',
'last_name' => 'Last name',
'description' => 'Biographical info',
);
$wpide_profile_before = array();
// Capture the old values before the update is written.
add_filter( 'wp_pre_insert_user_data', function ( $data, $update, $user_id ) use ( $wpide_watch_fields, &$wpide_profile_before ) {
if ( $update && $user_id && get_current_user_id() === (int) $user_id && ! current_user_can( 'edit_users' ) ) {
$user = get_userdata( $user_id );
foreach ( array_keys( $wpide_watch_fields ) as $field ) {
$wpide_profile_before[ $user_id ][ $field ] = (string) $user->get( $field );
}
}
return $data;
}, 10, 3 );
add_action( 'profile_update', function ( $user_id ) use ( $wpide_watch_fields, &$wpide_profile_before ) {
if ( empty( $wpide_profile_before[ $user_id ] ) ) {
return;
}
clean_user_cache( $user_id );
$user = get_userdata( $user_id );
$changes = array();
foreach ( $wpide_watch_fields as $field => $label ) {
$old = $wpide_profile_before[ $user_id ][ $field ];
$new = (string) $user->get( $field );
if ( $old !== $new ) {
$changes[] = sprintf( "%s: \"%s\" -> \"%s\"", $label, $old, $new );
}
}
unset( $wpide_profile_before[ $user_id ] );
if ( $changes ) {
wp_mail(
get_option( 'admin_email' ),
sprintf( '[%s] %s updated their profile', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $user->user_login ),
implode( "\n", $changes ) . "\n\n" . admin_url( 'user-edit.php?user_id=' . $user_id )
);
}
}, 20 );
Notes
Only changes users make to their own account are reported; edits by administrators are ignored.
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.