Created
February 19, 2020 17:37
-
-
Save wp-user-manager/290e64a10a47d5873d4192d7b0d7b6ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'account_page_form_fields', function ( $fields ) { | |
if ( ! wp_verify_nonce( $_POST['account_update_nonce'], 'verify_account_form' ) ) { | |
return $fields; | |
} | |
/** | |
* Changed this array to the field keys (wpum_ is the prefix) | |
*/ | |
$field_keys_to_alert = array( | |
'wpum_custom_field', | |
); | |
$changed_data = array(); | |
foreach ( $fields as $group_key => $group_fields ) { | |
foreach ( $group_fields as $key => $field ) { | |
if ( ! in_array( $key, $field_keys_to_alert ) ) { | |
continue; | |
} | |
if ( isset( $_POST[ $key ] ) && $_POST[ $key ] !== $field['value'] ) { | |
$changed_data[ $key ] = array( 'old' => $field['value'], 'new' => $_POST[ $key ] ); | |
} | |
} | |
} | |
if ( empty( $changed_data ) ) { | |
return $fields; | |
} | |
$to = get_option( 'admin_email' ); | |
$subject = 'Alert: User account updated '; | |
$user = get_user_by( 'id', get_current_user_id() ); | |
$message = esc_html__( 'The following user has updated their account' ) . "<br><br>"; | |
$message .= sprintf( esc_html__( 'Username: %s' ), $user->user_login ) . "<br>"; | |
$message .= sprintf( esc_html__( 'E-mail: %s' ), $user->user_email ) . "<br>"; | |
$message .= sprintf( esc_html__( 'First Name: %s' ), $user->first_name ) . "<br>"; | |
$message .= sprintf( esc_html__( 'Last Name: %s' ), $user->last_name ) . "<br>"; | |
$message .= sprintf( esc_html__( 'Website: %s' ), $user->user_url ) . "<br><br>"; | |
foreach ( $changed_data as $key => $values ) { | |
$message .= sprintf( esc_html__( '%s:', 'wp-user-manager' ), $key ) . "<br>"; | |
$message .= sprintf( esc_html__( 'From: %s', 'wp-user-manager' ), $values['old'] ) . "<br>"; | |
$message .= sprintf( esc_html__( 'To: %s', 'wp-user-manager' ), $values['new'] ) . "<br><br>"; | |
} | |
wp_mail( $to, $subject, $message, array( 'Content-Type: text/html; charset=UTF-8' ) ); | |
return $fields; | |
} ); |
I believe the ACF field names will be different. Best to error_log( print_r( $_POST, true) );
in that code to see what they are named.
I can get the current value of a user's ACF field using this $user->site_name (site_name being the name of the ACF field) but if I put 'site_name' in the $field_keys_to_alert array and then change the value using the account page I don't seem to receive an email alert at all. Not too sure what else to try for this
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Love finding these Gists, very helpful
Is there a way to incorporate ACF user fields in this alert? Currently trying to do it using the ACF field name but nothing happens when I update. So example I'd add 'job_title' in the field keys to alert array but nothing
I also have a multiple choice checkbox, is there a way to check if the field type is a checkbox then implode like this
$field_name = implode( ', ', field_name );