Title: Profile notification by email &#8211; Code for theme functions.php
Last modified: January 25, 2017

---

# Profile notification by email – Code for theme functions.php

 *  Resolved [mich3lang3lo](https://wordpress.org/support/users/mich3lang3lo/)
 * (@mich3lang3lo)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/profile-notification-by-email-code-for-theme-functions-php/)
 * Hello everyone,
 * I am trying to make the code below work with Profile Builder but with no sucess,
   could you guys take a look?
 * I want to receive email notification for any profile change.
 * **The original code:**
 *     ```
       /* ===========================================
           Send Emails when User Profile Changes
          =============================================*/
   
       // IF EMAIL CHANGES
       function sr_user_profile_update_email( $user_id, $old_user_data ) {
   
         $user = get_userdata( $user_id );
         if($old_user_data->user_email != $user->user_email) {
           $admin_email = "you@yourdomain.com";
           $message = sprintf( __( 'This user has updated their profile on the SchoolRise USA Staff Member site.' ) ) . "\r\n\r\n";
           $message .= sprintf( __( 'Display Name: %s' ), $user->display_name ). "\r\n\r\n";
           $message .= sprintf( __( 'Old Email: %s' ), $old_user_data->user_email ). "\r\n\r\n";
           $message .= sprintf( __( 'New Email: %s' ), $user->user_email ). "\r\n\r\n";
           wp_mail( $admin_email, sprintf( __( '[Staff Member Site] User Profile Update' ), get_option('blogname') ), $message );
         }
   
       }
   
       add_action( 'profile_update', 'sr_user_profile_update_email', 10, 2 );
   
       // IF PHONE NUMBER CHANGES
       function sr_user_profile_update_phone( $user_id, $old_user_data ) {
   
         $old_user_data = get_transient( 'sr_old_user_data_' . $user_id );
         $user = get_userdata( $user_id );
   
         if($old_user_data->phone != $user->phone) {
           $admin_email = "you@yourdomain.com";
           $message = sprintf( __( 'This user has updated their profile on the SchoolRise USA Staff Member site.' ) ) . "\r\n\r\n";
           $message .= sprintf( __( 'Display Name: %s' ), $user->display_name ). "\r\n\r\n";
           $message .= sprintf( __( 'Old Phone: %s' ), $old_user_data->phone ). "\r\n\r\n";
           $message .= sprintf( __( 'New Phone: %s' ), $user->phone ). "\r\n\r\n";
           wp_mail( $admin_email, sprintf( __( '[Staff Member Site] User Profile Update' ), get_option('blogname') ), $message );
         }
   
       }
   
       add_action( 'profile_update', 'sr_user_profile_update_phone', 10, 2 );
   
       // IF ADDRESS CHANGES
       function sr_user_profile_update_address( $user_id, $old_user_data ) {
   
         $old_user_data = get_transient( 'sr_old_user_data_' . $user_id );
         $user = get_userdata( $user_id );
   
         if($old_user_data->street != $user->street or $old_user_data->city != $user->city or $old_user_data->state != $user->state or $old_user_data->zip != $user->zip) {
   
           $admin_email = "you@yourdomain.com";
           $message = sprintf( __( 'This user has updated their profile on the SchoolRise USA Staff Member site.' ) ) . "\r\n\r\n";
           $message .= sprintf( __( 'Display Name: %s' ), $user->display_name ). "\r\n\r\n";
           $message .= sprintf( __( 'Old Address: %s, %s, %s %s' ), $old_user_data->street, $old_user_data->city, $old_user_data->state, $old_user_data->zip ). "\r\n\r\n";
           $message .= sprintf( __( 'New Address: %s, %s, %s %s' ), $user->street, $user->city, $user->state, $user->zip ). "\r\n\r\n";
           wp_mail( $admin_email, sprintf( __( '[Staff Member Site] User Profile Update' ), get_option('blogname') ), $message );
         }
   
       }
   
       add_action( 'profile_update', 'sr_user_profile_update_address', 10, 2 );
   
       // Save old user data and meta for later comparison for non-standard fields (phone, address etc.)
       function sr_old_user_data_transient(){
   
         $user_id = get_current_user_id();
         $user_data = get_userdata( $user_id );
         $user_meta = get_user_meta( $user_id );
   
         foreach( $user_meta as $key=>$val ){
           $user_data->data->$key = current($val);
         }
   
         // 1 hour should be sufficient
         set_transient( 'sr_old_user_data_' . $user_id, $user_data->data, 60 * 60 );
   
       }
       add_action('show_user_profile', 'sr_old_user_data_transient');
   
       // Cleanup when done
       function sr_old_user_data_cleanup( $user_id, $old_user_data ){
         delete_transient( 'sr_old_user_data_' . $user_id );
       }
       add_action( 'profile_update', 'sr_old_user_data_cleanup', 1000, 2 );
       ```
   
 * **And working with buddypress:**
 *     ```
       function rc_buddypress_profile_before_update_fields(){
   
   
                       $user_id = get_current_user_id();
   
                       $user_data = array();
   
                       $user_data['First Name'] = bp_get_profile_field_data('field=First Name') ;
                       $user_data['Last Name'] = bp_get_profile_field_data('field=Last Name') ;
                       $user_data['Spouse'] = bp_get_profile_field_data('field=Spouse') ;
                       $user_data['Address Line 1'] = bp_get_profile_field_data('field=Address Line 1') ;
                       $user_data['Address Line 2'] = bp_get_profile_field_data('field=Address Line 2') ;
                       $user_data['City, State Zip'] = bp_get_profile_field_data('field=City, State Zip') ;
                       $user_data['Country'] = bp_get_profile_field_data('field=Country') ;
                       $user_data['City/Locality'] = bp_get_profile_field_data('field=City/Locality') ;
                       $user_data['Zip/Postal Code'] = bp_get_profile_field_data('field=Zip/Postal Code') ;
                       $user_data['Email Address'] = bp_get_profile_field_data('field=Email Address') ;
                       $user_data['Home Phone'] = bp_get_profile_field_data('field=Home Phone') ;
                       $user_data['Office Phone'] = bp_get_profile_field_data('field=Office Phone') ;
                       $user_data['Mobile Phone'] = bp_get_profile_field_data('field=Mobile Phone') ;
                       $user_data['Fax'] = bp_get_profile_field_data('field=Fax') ;
                       $user_data['Fellowship Area'] = bp_get_profile_field_data('field=Fellowship Area') ;
                       $user_data['Area Representative'] = bp_get_profile_field_data('field=Area Representative') ;
                       $user_data['EF Credential'] = bp_get_profile_field_data('field=EF Credential') ;
   
   
   
           if ( false === ( $value = get_transient( 'sr_old_data_' . $user_id ) ) ) {  // check if already transient created because this hook is being called for every field.
   
               set_transient( 'sr_old_data_' . $user_id, serialize($user_data), 60 * 60 );
   
           }
   
   
        }
   
       // IF ANY FIELD CHANGES
       function rc_buddypress_profile_update_fields( $user_id ) { 
   
   
               $old_data = unserialize(get_transient( 'sr_old_data_'.$user_id ));  //fetch old data from transient
               delete_transient( 'sr_old_data_' . $user_id );  //delete transient 
   
               $admin_email = "youremail@yourdomain.com";
   
               /* count number of changes*/
               $changes=0;
               foreach($old_data as $key => $value){
   
   
                   $newvalue = bp_get_profile_field_data('field='.$key);
   
                   if($value != $newvalue) 
                   { 
                       $changes++;
                   }
               }
   
               $message = sprintf( __( '%1$s has done following changes:', 'buddypress' ), bp_core_get_user_displayname( $user_id ) ) . "\r\n\r\n";;
               foreach($old_data as $key => $value){
   
                   if($changes == 1)  // if only one change
                   {
   
                       $newvalue = bp_get_profile_field_data('field='.$key);
   
                       if($value != $newvalue) 
                       { 
                           $message = sprintf( __( '%1$s has changed '.$key.':', 'buddypress' ), bp_core_get_user_displayname( $user_id ) ) . "\r\n\r\n"; 
                           $message .= sprintf( __( ' Old '.$key.': %1$s', 'buddypress' ), ( $value ) ) . "\r\n\r\n"; 
                           $message .= sprintf( __( ' New '.$key.': %1$s', 'buddypress' ), bp_get_profile_field_data( 'field='.$key ) ) . "\r\n\r\n"; 
                       }
                   }
                   else  // if more than one change
                   {
                       $newvalue = bp_get_profile_field_data('field='.$key);
   
                       if($value != $newvalue) 
                       { 
                           $message .= sprintf( __( '-> Old '.$key.': %1$s', 'buddypress' ), ( $value ) ) . "\r\n\r\n"; 
                           $message .= sprintf( __( '   New '.$key.': %1$s', 'buddypress' ), bp_get_profile_field_data( 'field='.$key ) ) . "\r\n\r\n"; 
                       }
   
                   }   
               }
   
   
   
   
           wp_mail( $admin_email, sprintf( __( 'EF Members Only User Profile Update' ), get_option('blogname') ), $message );
       }
   
       add_action( 'xprofile_updated_profile', 'rc_buddypress_profile_update_fields', 10, 5 ); 
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Teodor Cosofret](https://wordpress.org/support/users/teodor-cosofret/)
 * (@teodor-cosofret)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/profile-notification-by-email-code-for-theme-functions-php/#post-8701804)
 * Hi,
 * To notify the Admin when a user updates his profile use this [custom code](https://www.cozmoslabs.com/docs/profile-builder-2/developers-knowledge-base/email/email-administrator-successful-edit-profile/).
 * Best regards,
 *  Thread Starter [mich3lang3lo](https://wordpress.org/support/users/mich3lang3lo/)
 * (@mich3lang3lo)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/profile-notification-by-email-code-for-theme-functions-php/#post-8701833)
 * Thank you [@teodor-cosofret](https://wordpress.org/support/users/teodor-cosofret/),
 * You are the best.
 * take care
 * Mike
 *  Thread Starter [mich3lang3lo](https://wordpress.org/support/users/mich3lang3lo/)
 * (@mich3lang3lo)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/profile-notification-by-email-code-for-theme-functions-php/#post-8703016)
 * I am getting an error after using the custom code [@teodor-cosofret](https://wordpress.org/support/users/teodor-cosofret/),
   could you take a look?
 * `Parse error: syntax error, unexpected end of file in /home/fazen735/public_html/
   wp-content/plugins/profile-builder/front-end/class-formbuilder.php on line 737`
 * On line 737 i have:
 * `do_action( 'wppb_before_'.$this->args['form_type'].'_fields', $this->args['form_name'],
   $this->args['ID'], $this->args['form_type'] );`
 * I am using the plugin WP-Mail-SMTP and is working with the default WP profile
   page, i get all notifications.
 * Ty
 * Mike
 *  [Teodor Cosofret](https://wordpress.org/support/users/teodor-cosofret/)
 * (@teodor-cosofret)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/profile-notification-by-email-code-for-theme-functions-php/#post-8714748)
 * Hello Mike,
 * In order to debug this issue further please submit a [support ticket](https://www.cozmoslabs.com/support/).
   I will wait for your ticket and we will continue our conversation there.
 * Also I will close this support thread – [https://wordpress.org/support/topic/not-working-with-better-notifications-for-wordpress/#post-8702985](https://wordpress.org/support/topic/not-working-with-better-notifications-for-wordpress/#post-8702985)–
   since it addresses the same issue.
 * Best regards,

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Profile notification by email – Code for theme functions.php’ is closed
to new replies.

 * ![](https://ps.w.org/profile-builder/assets/icon-256x256.png?rev=2961144)
 * [User Profile Builder - Beautiful User Registration Forms, User Profiles & User Role Editor](https://wordpress.org/plugins/profile-builder/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/profile-builder/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/profile-builder/)
 * [Active Topics](https://wordpress.org/support/plugin/profile-builder/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/profile-builder/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/profile-builder/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [Teodor Cosofret](https://wordpress.org/support/users/teodor-cosofret/)
 * Last activity: [9 years, 4 months ago](https://wordpress.org/support/topic/profile-notification-by-email-code-for-theme-functions-php/#post-8714748)
 * Status: resolved