• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Store
    • Cart
    • Checkout
  • Blog
    • Basics [Free]
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Contact
  • Sign In
  • Show Search
Hide Search
Home » Tips and Tricks » Email Admin When a User Updates Registration Data

Email Admin When a User Updates Registration Data

Chad Butler · Apr 26, 2013 ·

Here’s a request I get from time to time – how can the admin receive email notification that a user updated their data, so I figured it is about time to put that together as a tutorial.

This one should be fairly straight forward and can be used cut-and-paste for the general features.  As usual, I’ve tried to comment the code so that you know what is happening where and more advanced users can build on this concept from there.  

This article is only available to WP-Members Support Subscribers. If you have an existing subscription, please login below. If you do not have a current support subscription, you can purchase a support subscription here.

Already a Member? Log In Here
   
Forgot password? Click here to reset

To gain full access to WP-Members premium content, a current support subscription is required. You can purchase an annual support subscription for as little as $59, which provides you with access to priority support, a customer support forum, access to code snippets, and more.

Why wait? Choose your subscription option here.

[
Why join?]

Tips and Tricks actions, add_action, wpmem_post_update_data, wpmem_pre_update_data, wp_mail

Welcome to RocketGeek Interactive › Forums › Email Admin When a User Updates Registration Data

Tagged: actions, add_action, wpmem_post_update_data, wpmem_pre_update_data, wp_mail

  • This topic has 20 replies, 8 voices, and was last updated 1 year, 10 months ago by Ryan@socialscientist.co.za.
Viewing 12 reply threads
  • Author
    Posts
    • April 26, 2013 at 10:20 am #2479
      Chad Butler
      Keymaster

      Here’s a request I get from time to time – how can the admin receive email notification that a user updated their data, so I figured it is about time
      [See the full post at: Email Admin When a User Updates Registration Data]

    • July 20, 2013 at 9:13 pm #2823
      slamack
      Participant

      This worked perfect for me!  You ROCK, Chad!  Thank you!

    • August 2, 2013 at 10:26 am #2900
      Chad Butler
      Keymaster

      Awesome!

    • August 18, 2014 at 2:58 pm #5671
      Omarben
      Participant

      I tried to insert my same phone number validate code here to fix phone number to my format.It looks like $fields is not written back

      How do I modify $field[‘phone1]’ or better where is phone1 saved in wpmem_pre_update_data

      add_action(‘wpmem_pre_update_data’, ‘check_user_data’, 10, 1);
      function check_user_data($fields)
      {

      if (!jb_validate_phone_number($fields[‘phone1’])) {
      global $wpmem_themsg;
      $wpmem_themsg = ‘You must enter a valid phone number’;
      return;
      }
      // Format Phone Number
      preg_match(‘/^[+]?([\d]{0,3})?[\(\.\-\s]?([\d]{3})[\)\.\-\s]*([\d]{3})[\.\-\s]?([\d]{4})$/’, $fields[‘phone1’], $matches);
      $fixed = $matches[2] . ‘-‘ . $matches[3] . ‘-‘ . $matches[4];

      $fields[‘phone1’] = $fixed;

      // globalize the $chk_fields and $userdata arrays

      global $chk_fields, $userdata;

      // get an array of the field names
      $wpmem_fields = get_option(‘wpmembers_fields’);

      /**
      * get the original data in the $chk_fields array
      *
      * assuming that we are concerned with wp_usermeta
      * and only email from wp_users (can’t change username)
      */
      for ($row = 0; $row < count($wpmem_fields); $row++) {

      if ($wpmem_fields[$row][2] == ‘user_email’) {
      $chk_fields[$wpmem_fields[$row][2]] = $userdata->user_email;
      } else {
      $chk_fields[$wpmem_fields[$row][2]] = get_user_meta($fields[‘ID’], $wpmem_fields[$row][2], true);
      }
      }
      }

    • February 25, 2015 at 9:47 pm #7278
      JoeDostie
      Participant

      Hi Chad,

      As usual this plugin and support has been excellent and so flexible!

      I would like to use this and I have been doing my best to understand it.

      I would like to modify it a little in the following way but every time I touch it I get lost and have to start over…

      What I would like to do is instead of checking “everything” and see what has changed I would like to instead define a single field – aka – Option Name(s).

      I want to check on one field called “volunteer_apply” so if they modify that, then it can fire off an email.

      They can modify their address and phone number to their hearts content and I don’t want to know about those.

      I still want to leave the array like you have, even though I am checking a single item, because I can add more fields to the array later in life if I need to.

      I hope this makes sense as my mind is fried now! 🙂

      Thanks again!

    • February 25, 2015 at 9:56 pm #7279
      JoeDostie
      Participant

      OK, of course after you hit the submit button is when there is a little more clarity…

      41 foreach( $fields as $key => $val ) {
      42 if( $key != ‘ID’ && $val != $chk_fields[$key] ) {

      So instead of looping through the array, I would instead want to define what item(s) in the array I want to look at (instead of all of them).

      • February 25, 2015 at 11:19 pm #7284
        Chad Butler
        Keymaster

        What you want to do is actually much easier because you can dispense with checking what all the fields were originally to compare to what they are after the user updates.

        — see this post for a better answer —

    • February 26, 2015 at 12:14 pm #7290
      JoeDostie
      Participant

      Thank you so much for that code.
      As usual I am impressed with the speed, knowledge and support!

      This will solve the initial objective, but as I mentioned, what happens when I add a second field (or 3rd or 4th) as my requirements change in the future. I think this code will require several areas to update, still a little to confusing to me.

      I was thinking about defining a global array at the beginning like this:

      
      fields_to_check = array
      (
      	$volunteer_apply => 'volunteer_apply',
      	$some_other_value =>'Option Name',
      	/* and so on */
      (
      

      That way I could add/remove in only the array without touching your existing code as fields are added or removed.

      Does this sound reasonable or am I off my rocker?

      Been in the computer field for 20 years but I’m still learning PHP 🙂

      Thank you again!

      • February 26, 2015 at 1:43 pm #7293
        Chad Butler
        Keymaster

        Actually, based on that and also thinking about it a little bit, I will change my answer slightly. This code is a similar concept, but still puts the custom fields in an array. That array could be one value (for considering only one field) or multiple for as many more as you need.

        Also, by setting that up at the very beginning, it should be the only thing you need to change (as opposed to the original single version where you would have needed to change any instance of the field’s option name.

        So all you really need to do here is change the values in $my_fields:

        add_action( 'wpmem_pre_update_data', 'check_user_data' );
        function check_user_data( $fields ) {
        	global $field_data;
        	
        	/**
        	 * Set up an array of the field option names
        	 * for the fields you want to track changes on
        	 */
        	$my_fields = array( 
        		'volunteer_apply',
        		'my_other_field',
        	);
        	
        	/**
        	 * This will build an array of field values using
        	 * the field option names you defined above, putting
        	 * them in the array $field_data with the key => value
        	 * as 'option_name' => 'value'
        	 */
        	$field_data = array();
        	foreach ( $my_fields as $my_field ) {
        		$field_data[ $field ] = get_user_meta( $fields['ID'], $my_field, true );
        	}
        }
        
        add_action( 'wpmem_post_update_data', 'verify_user_data' );
        function verify_user_data( $fields ) {
        	// globalize the $volunteer_apply variable and the $userdata
        	global $field_data, $userdata;
        	
        	// set $do_email to false
        	$do_email = false;
        	
        	/**
        	 * compare posted data to original data and put it into 
        	 * an email $body variable
         	 */
        	$body = '';
        	foreach ( $field_data as $key => $val ) {
        		if ( $fields[ $key ] != $val ) { 
        
        			/**
        			 * put change in the email body
        			 */
        			$body.= 'Volunteer Apply was updated from ' . $val . ' to ' . $fields[ $key ] . "\r\n";
        
        			// toggle $do_mail
        			$do_email = true;
        		}
        	}
        	
        	/**
        	 * if $do_email remained false, nothing was changed.
        	 * otherwise, send email notification to admin
         	 */
        	if ( $do_email ) {
        	
        		$subj = "User data update";
        		$body = $userdata->user_login . " changed the following registration data: \r\n \r\n" . $body;
        	
        		/**
        		 * This section gets a little fancy - all you really need is to 
        		 * use wp_mail.  But what if your setup is using some of the handy
        		 * WP-Members filter/action hooks for emails, like email from name
        		 * and the admin notifcation address filter?  This includes the 
        		 * needed elements to apply those same general hooks here.
        	 	 */
        		include_once( WPMEM_PATH . 'wp-members-email.php' );
        		/* Apply filters (if set) for the sending email address */
        		add_filter( 'wp_mail_from', 'wpmem_mail_from' );
        		add_filter( 'wp_mail_from_name', 'wpmem_mail_from_name' );
        		/* Get the admin's email address and filter headers */
        		$admin_email = apply_filters( 'wpmem_notify_addr', get_option( 'admin_email' ) );
        		$headers     = apply_filters( 'wpmem_email_headers', '' );
        		
        		
        		// lastly, send the message
        		wp_mail( $admin_email, stripslashes( $subj ), stripslashes( $body ), $headers );
        	
        	}
        }
    • February 26, 2015 at 4:57 pm #7298
      JoeDostie
      Participant

      This is awesome!!!

      I cant thank you enough.

      I hope other members can get some use out of this.

      • February 27, 2015 at 10:16 am #7300
        Chad Butler
        Keymaster

        No Problem.

        This is a prime example of how the discussion leads to improvements on the original ideas. While a lot of the examples on the site stem from a user related request, it’s often just my interpretation of implementation. From there, other users try it out and, like you, have a similar but not quite exact need, which leads to improvement of the original idea.

        (The last part being, if I remember to do it, expanding these revisions into their own posts/tutorials/articles.)

    • June 8, 2015 at 5:01 pm #8050
      Mintstone
      Participant

      Hi Chad,
      I pasted the above code to my child theme functions.php file but did not get an email when i tested a change of member data.
      Any idea what might be going wrong?
      Could it be WPMEM_PATH not being set?
      Cheers, Anne

      • June 8, 2015 at 9:46 pm #8052
        Chad Butler
        Keymaster

        Hi Anne,

        WPMEM_PATH is a constant established by the plugin. So if WP-Members is activated, it would be set with the path to the plugin folder. So I don’t think that’s the problem.

        Are other emails being sent? Any issues with the admin notification emails or new user registration emails?

    • August 5, 2015 at 1:29 pm #8365
      zonkeroo
      Blocked

      Just FYI-
      Had a problem with this until I went to look for the include file.
      I saw no reference to this being updated under your release info page either..
      The include:
      include_once( WPMEM_PATH . 'wp-members-email.php' );
      doesn’t exist in version 3.0.2 – at least I do not see it.
      I found an email.php with the needed data here:
      include_once( WPMEM_PATH . 'inc/email.php' );
      now above code works for me and uses fields in email setup instead of defaults…

      thanks-
      Brady S.

      • November 4, 2015 at 4:12 pm #8849
        Chad Butler
        Keymaster

        Sorry for the delayed response – I unfortunately did not see this post until now.

        Thanks for posting this. Yes, the email file (as well as several others) were moved in version 3.0. These are now in the /inc/ folder.

        Unfortunately, some of these older posts (this one is from 2013 originally) slip through the cracks when these updates happen. I honestly try to keep things as current as possible on the site and also I am very cautious about changes to code, files, etc because I want to maintain as much backward compatibility as possible. But sometimes an item might get missed (like updating this post).

        I’ve updated the code in the example (and also did a little clean up at the same time).

    • April 9, 2018 at 5:44 am #14562
      pixellent
      Participant

      Hi…
      I tried the above code but it seems I can’t get any email when there is a user edited their profile..

      Any hints? Thanks…

    • April 9, 2018 at 5:46 am #14563
      pixellent
      Participant

      Oops, sorry, I got the email after few mins…Thanks a lot!

      • April 9, 2018 at 8:06 am #14564
        Chad Butler
        Keymaster

        No problem – glad it worked out for you.

        For anyone else having issues with receiving any emails, there is some good info on troubleshooting email in the FAQs:
        https://rocketgeek.com/plugins/wp-members/docs/faqs/#email

    • April 1, 2019 at 8:18 am #17532
      Ryan@socialscientist.co.za
      Participant

      Hey Chad,

      I have gotten this partly right.
      Pasted the code as is into my functions.php

      I then experimented by changing just a name field.

      I got the notification but along with the name change, there was some other “changes as well” i.e there werent any actual other changes but it is some there were?

      Please see attached what comes through on the email

      _________________________

      ryan_k0aw97tj changed the following registration data:
      <br>first_name was updated from R.Y.A.N to Hello
      IDcopyupload was updated from 3173 to
      SHGreg was updated from 3174 to
      compregcert was updated from 3175 to
      VATregCert was updated from 3176 to
      WeighBCal was updated from 3177 to

      • April 2, 2019 at 3:26 pm #17545
        Chad Butler
        Keymaster

        Do you have some other custom actions that are occurring on the user profile update? Possibly hooked to ‘wpmem_post_update_data’ but not limited to just that?

        • April 7, 2019 at 1:29 pm #17587
          Ryan@socialscientist.co.za
          Participant

          Not that I am aware of. I am pasting the code for you

          add_action( 'wpmem_pre_update_data', 'check_user_data' );
          function check_user_data( $fields ) {
              // Globalize the $chk_fields and $userdata arrays.
              global $chk_fields, $userdata;
          
              /*
               * Get the original data in the $chk_fields array.
               *
               * Assuming that we are concerned with wp_usermeta
               * and only email from wp_users (can't change username),
                */
              foreach ( wpmem_fields() as $meta_key => $field ) {
                  
                  if ( $meta_key == 'user_email' ) {
                      $chk_fields[ $meta_key ] = $userdata->user_email;
                  } else {
                      $chk_fields[ $meta_key ] = get_user_meta( $fields['ID'], $meta_key, true );
                  }
              }
          }
          
          add_action( 'wpmem_post_update_data', 'verify_user_data' );
          function verify_user_data( $fields ) {
              
              // Globalize the $chk_fields and $userdata arrays.
              global $chk_fields, $userdata;
              
              // Set $do_email to false,
              $do_email = false;
              
              /*
               * Compare posted data to original data.
               *
               * Note: ID is only in $chk_fields and would not
               * be user editable anyway, so we skip that.
                */
              foreach ( $fields as $key => $val ) {
                  if ( $key != 'ID' && $val != $chk_fields[ $key ] ) { 
                      
                      /*
                       * Build changes for email body. this puts each change in a new line:
                       * "field name" was updated from "old value" to "new value".
                        */
                      $body.= $key . ' was updated from ' . $chk_fields[ $key ] . ' to ' . $fields[ $key ] . "\r\n";
                      
                      // Toggle $do_mail.
                      $do_email = true;
                  }
              }
              
              /*
               * If $do_email remained false, nothing was changed.
               * otherwise, send email notification to admin.
                */
              if ( $do_email ) {
              
                  $subj = "User data update";
                  $body = $userdata->user_login . " changed the following registration data: \r\n \r\n <br>" . $body;
                  
                      // Apply filters (if set) for the sending email address.
                  add_filter( 'wp_mail_from', 'wpmem_mail_from' );
                  add_filter( 'wp_mail_from_name', 'wpmem_mail_from_name' );
                  
                  // Get the admin's email address and filter headers.
                  $admin_email = apply_filters( 'wpmem_notify_addr', get_option( 'admin_email' ) );
                  $headers     = apply_filters( 'wpmem_email_headers', '' );
                  
                  // Lastly, send the message.
                  wp_mail( $admin_email, stripslashes( $subj ), stripslashes( $body ), $headers );
              
              }
          } // end of verify_user_data()
          /*ADDING TEXT INTO THE FORM*/
          • This reply was modified 1 year, 10 months ago by Chad Butler.
  • Author
    Posts
Viewing 12 reply threads
  • You must be logged in to reply to this topic.
Log In

Ready to get started?

Join Today!

© 2021 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Refund Policy