Support

Account

Home Forums ACF PRO Admin email notification on change of User ACF Field

Helping

Admin email notification on change of User ACF Field

  • I am using ACF Pro in combination with a membership plugin WP User Manager, I have added a field group to users but I am trying to trigger an admin notification for a user updating their ACF fields.

    I have managed to rip some code from the plugin but this is for a user updating custom fields the plugin supports and not ACF fields.

    I have tried to change the $field_keys_to_alert array to the acf field key value and also the acf field name but neither seem to work. Has anyone ever done something similiar?

    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;
    } );
  • To detect changes in ACF field values you must use an acf/save_post action with a priority of < 10. In this filter you need to compare the values in $_POST[‘acf’] with the corresponding field values by using get_field().

    It appears that code is doing much the same thing, but I cannot say if that function is running before ACF saves values.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.