Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.