Posted on by

Sending an Email when a Record is Updated

Sometimes, you want to send a message to the person who created a record when their record is updated by an administrator. For example, let’s say you use Participants Database for a registration that requires approval by an admin before it can be used. You want to let the person who submitted the registration know when their registration was approved by sending them an email.

This is a practical application of the Template Email class included in Participants Database. Take a look at this article for some background on that:

Using the PDb_Template_Email Class 

We’re going to take the example used in that article and wrap it in a simple plugin to get our functionality.

Using the Hook

To trigger sending the email, we use the plugin filter ‘pdb_before_submit_update’ which passes in the record data that will be used to update the record. This hook is triggered both when a record is updated on the frontend (typically when a user comes to the site with their private link and edits their record) and when a record is updated in the admin section. It’s easy to tell which it is by testing whether the event takes place in the context of the admin section or not.

We set up the filter like this so that it calls our function and does it’s thing:

add_filter( 'pdb-before_submit_update', 'pdb_send_record_update_notification' );

In that statement, we set up the filter to call a function named ‘pdb_send_record_update_notification’ to add our functionality. The first thing our function does is check to see if we should send the email by making sure the event is triggered in the admin section and that there is a valid email address to send the message to. If not, return the data (so it can be saved) and we’re done.

 if ( ! is_admin() || empty( $post['email'] ) ) {
  return $post;
 }

Next, if we are going to send the email, we need to define the subject and body of the email:

 $subject = 'Your record has been updated';
 $message = 'Dear [first_name] [last_name],

Your record has been updated.';

You’ll need to modify these strings to what you want in your message email. You can use any placeholder tags you want to fill in the message, it works the same as the email setting in the main plugin. Next, define the “from” field: this is very important, it should be an email address form the same domain as your website to avoid the message looking like spam.

$from = 'Your Website <info@yourwebsite.com>';

Now, all we need to do is put it all together into a configuration array so we can use the Template Email class to send the message:

 $config = array(
   'to' => $post['email'],
   'from' => $from,
   'subject' => $subject,
   'template' => $message,
 );

The email class just needs this configuration data and the record data (so it can fill in the placeholders) and it will send the email:

PDb_Template_Email::send( $config, $post );

Putting it Into a Plugin

Wrapping all this into a plugin is a good way to go because you can easily turn it on and off by activating/deactivating the plugin. Also, if you change themes, the functionality will continue to work. To create the plugin, create a php file on your computer, name it ‘pdb_update_notify.php’ You will upload this file to the “plugins” directory in your WordPress. Once you’ve done that, you can activate it in the plugin menu.

Here is the plugin code:

That’s it. There are many variations on this plugin you can easily incorporate. For instance, you may want to ensure that this email only gets sent once. You could do that by using the ID of the incoming array to load the current record data. Now, you can check a field value in both arrays (the incoming array and the one you got for the current record) to see if a particular one was changed. That can tell you if the email should be sent or not.

19 thoughts on “Sending an Email when a Record is Updated

  1. HI
    Great thanks for great plugin
    Some of my records are in a group, I would like to send to custom selected email to a group when for example the last eight people record changed ?
    Could you please help
    also I really dont know how to place edit record in frontend
    Thanks
    John

    1. First, you must define your email template under the PDb Email Templates menu. Set the “send action” to “PDb Admin List With Selected Action” Define the rest of the email template as you need.

      On the “List Participants” page in the Participants Database menu, you can sort your records by the date they were last accessed by the user. Select the ones you want to send the email to using the checkbox on the left. Under the “with selected” dropdown at the top left of the list, select the email template you defined. Now you can send this email to all the selected people.

      1. Where is the PDB Email Templates Menu?

        1. This is referring to the Email Expansion Kit premium add-on. Click the link for more info.

  2. […] few months ago, I wrote a tutorial on how to automatically send an email when a record is updated. I’ve gotten several requests for something very similar: how to send an email to a person […]

  3. dear Roland – many many tanks for the great job. This looks pretty amazing.

    The idea of sending an e-mail to an user is very fascinating. I guess that many many users love to have these features: in combination
    An alert function that provides new shortcode to add pages which gives logged in users an alerts management page to add, preview, enable, disable, edit and delete email alerts. Alerts can be configured based on keywords and data entry-types of the participant database. An idea would be: it creates email alerts based on searches using the participants-database filters to search for entries of the
    participants-database.

    Example: If a user is logged in, he can save his search as an alert using the ‘add alert’ button. This could lead to the following results: Daily, weekly & fortnightly email alerts:
    Email alerts can be configured to be sent with several different intervals triggered by WordPress cron.
    Alerts are sent in plain text format and list all matching jobs posted during the interval.

    Dear Roland: what do you think bout these ideas and features:

    BTW: this alert-ideas is inspired by the job alert plugin and his addons… job-alerts: https://wpjobmanager.com/add-ons/job-alerts/ i for one would be happy to see this addon – and i guess that many many people would be very glad too…

    regards martin

    1. Yes, these are good ideas…I’ve had something of this sort in the idea queue for a while now. You’ve added a few new ideas, so thanks!

  4. I copied and pasted your code, changed as noted (including the updated line of code), and spent several hours trying to figure out why it wasn’t working. I’m on Windows 7, using Chrome, and created the plugin over FTP using WinSCP. Activating the plugin, the Update Notification which was set in the Settings admin panel discontinued working.

    It finally dawned on me that the single quotes copied into the plugin as Windows “smart” single quotes instead of plain text. Changing them fixed the Update Notification set in Settings.

    That didn’t fix the back-end Update Notification.

    Then I tried Rob’s posted code — if ( is_admin() || ! ( $post[’email’] ) ) { — the back-end notifications started working again. Changed those single quotes to plain text, and the back-end notification started working again.

    I still haven’t gotten the plugin to send an additional email, but at least the standard Update Notification isn’t broken, so I may be closer.

    Sharing in case anyone else “broke” their notifications.

    1. Those smart quotes can be trouble if they get into your code, certainly. It’s best to use notepad or some other text editor that is made for coding.

      1. Right; which is why I was able to figure out they were the problem: WinSCP uses its own plain text editor, and the quotes didn’t copy over as plain text quotes.

  5. Excellent, thanks very much for doing this. If I want someone to edit from the front end and receive an email what modification would I make as I have tried removing the admin part but it does not send an email.

    1. Rob, thanks your question got me to take a closer look at the code and see my error. The error has been fixed. If you want to send the emails from the front end, the first line of the function should be:

      if ( !is_admin() && !empty( $post['email'] ) ) {

      1. Unfortunately I still cannot get this work for me. I get no email notification. I am editing from the front end (not the admin section) but no email is being sent to the registered email address.

      2. and to follow up on that, I cannot get it to work through the admin page either.

        1. Are you getting other emails from the plugin? Are you using the latest version of the plugin? It’s not giving me any trouble, so to help you, I’ll need to figure out what is different on your system.

        2. I will check tonight to see if it is latest. Thanks for the help so far.

        3. Roland. I am running the latest version Version 1.6.2.8. I get other emails (for example emails on signup). I have cut and paste the code from this page, with relevant changes to the editable bits like my email etc, and activated it with wordpress. When I create a record the relevant emails are sent. When I update there is no email sent.

          Does this require the button “Send Record Form Update Notification Email” to be checked? If I check it I only receive an email to the emails listed in the notify section of the signup plugin and not the person who set the record up.

          I have been through it again and again and cannot work out why it does not work?

          Here is the code I have used:
          $post[’email’],
          ‘from’ => $from,
          ‘subject’ => $subject,
          ‘template’ => $message,
          );
          // send the email
          PDb_Template_Email::send( $config, $post );
          /*
          * return the record data so it can be saved
          */
          return $post;
          }

        4. There is an error in your code. The configuration array for the email should be something like this:

          $config = array(
          'to' => $post[’email’],
          ‘from’ => $from,
          ‘subject’ => $subject,
          ‘template’ => $message,
          );

        5. Ok so I sorted it. I changed the line of the code to this:

          if ( is_admin() || ! ( $post[’email’] ) ) {

          which allowed me to get an update email to the signup address. Thanks.

Leave a Reply
You have to agree to the comment policy.

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.

19 thoughts on “Sending an Email when a Record is Updated

  1. HI
    Great thanks for great plugin
    Some of my records are in a group, I would like to send to custom selected email to a group when for example the last eight people record changed ?
    Could you please help
    also I really dont know how to place edit record in frontend
    Thanks
    John

    1. First, you must define your email template under the PDb Email Templates menu. Set the “send action” to “PDb Admin List With Selected Action” Define the rest of the email template as you need.

      On the “List Participants” page in the Participants Database menu, you can sort your records by the date they were last accessed by the user. Select the ones you want to send the email to using the checkbox on the left. Under the “with selected” dropdown at the top left of the list, select the email template you defined. Now you can send this email to all the selected people.

      1. Where is the PDB Email Templates Menu?

        1. This is referring to the Email Expansion Kit premium add-on. Click the link for more info.

  2. […] few months ago, I wrote a tutorial on how to automatically send an email when a record is updated. I’ve gotten several requests for something very similar: how to send an email to a person […]

  3. dear Roland – many many tanks for the great job. This looks pretty amazing.

    The idea of sending an e-mail to an user is very fascinating. I guess that many many users love to have these features: in combination
    An alert function that provides new shortcode to add pages which gives logged in users an alerts management page to add, preview, enable, disable, edit and delete email alerts. Alerts can be configured based on keywords and data entry-types of the participant database. An idea would be: it creates email alerts based on searches using the participants-database filters to search for entries of the
    participants-database.

    Example: If a user is logged in, he can save his search as an alert using the ‘add alert’ button. This could lead to the following results: Daily, weekly & fortnightly email alerts:
    Email alerts can be configured to be sent with several different intervals triggered by WordPress cron.
    Alerts are sent in plain text format and list all matching jobs posted during the interval.

    Dear Roland: what do you think bout these ideas and features:

    BTW: this alert-ideas is inspired by the job alert plugin and his addons… job-alerts: https://wpjobmanager.com/add-ons/job-alerts/ i for one would be happy to see this addon – and i guess that many many people would be very glad too…

    regards martin

    1. Yes, these are good ideas…I’ve had something of this sort in the idea queue for a while now. You’ve added a few new ideas, so thanks!

  4. I copied and pasted your code, changed as noted (including the updated line of code), and spent several hours trying to figure out why it wasn’t working. I’m on Windows 7, using Chrome, and created the plugin over FTP using WinSCP. Activating the plugin, the Update Notification which was set in the Settings admin panel discontinued working.

    It finally dawned on me that the single quotes copied into the plugin as Windows “smart” single quotes instead of plain text. Changing them fixed the Update Notification set in Settings.

    That didn’t fix the back-end Update Notification.

    Then I tried Rob’s posted code — if ( is_admin() || ! ( $post[’email’] ) ) { — the back-end notifications started working again. Changed those single quotes to plain text, and the back-end notification started working again.

    I still haven’t gotten the plugin to send an additional email, but at least the standard Update Notification isn’t broken, so I may be closer.

    Sharing in case anyone else “broke” their notifications.

    1. Those smart quotes can be trouble if they get into your code, certainly. It’s best to use notepad or some other text editor that is made for coding.

      1. Right; which is why I was able to figure out they were the problem: WinSCP uses its own plain text editor, and the quotes didn’t copy over as plain text quotes.

  5. Excellent, thanks very much for doing this. If I want someone to edit from the front end and receive an email what modification would I make as I have tried removing the admin part but it does not send an email.

    1. Rob, thanks your question got me to take a closer look at the code and see my error. The error has been fixed. If you want to send the emails from the front end, the first line of the function should be:

      if ( !is_admin() && !empty( $post['email'] ) ) {

      1. Unfortunately I still cannot get this work for me. I get no email notification. I am editing from the front end (not the admin section) but no email is being sent to the registered email address.

      2. and to follow up on that, I cannot get it to work through the admin page either.

        1. Are you getting other emails from the plugin? Are you using the latest version of the plugin? It’s not giving me any trouble, so to help you, I’ll need to figure out what is different on your system.

        2. I will check tonight to see if it is latest. Thanks for the help so far.

        3. Roland. I am running the latest version Version 1.6.2.8. I get other emails (for example emails on signup). I have cut and paste the code from this page, with relevant changes to the editable bits like my email etc, and activated it with wordpress. When I create a record the relevant emails are sent. When I update there is no email sent.

          Does this require the button “Send Record Form Update Notification Email” to be checked? If I check it I only receive an email to the emails listed in the notify section of the signup plugin and not the person who set the record up.

          I have been through it again and again and cannot work out why it does not work?

          Here is the code I have used:
          $post[’email’],
          ‘from’ => $from,
          ‘subject’ => $subject,
          ‘template’ => $message,
          );
          // send the email
          PDb_Template_Email::send( $config, $post );
          /*
          * return the record data so it can be saved
          */
          return $post;
          }

        4. There is an error in your code. The configuration array for the email should be something like this:

          $config = array(
          'to' => $post[’email’],
          ‘from’ => $from,
          ‘subject’ => $subject,
          ‘template’ => $message,
          );

        5. Ok so I sorted it. I changed the line of the code to this:

          if ( is_admin() || ! ( $post[’email’] ) ) {

          which allowed me to get an update email to the signup address. Thanks.

Leave a Reply
You have to agree to the comment policy.

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.