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.
hello sir,
i am using your plugin for a family event. everything is working as you have set it up except Update Notify Email. i downloaded the php file and placed in plugins and activated.
I have to update a record . it gets updated but email does not go to the record email but comes to my admin email . i have ticked Send Signup Notification Email and then it requires Signup Notification Recipients. now i have to put each person’s email for every single update because it is not accepting the field name [email] in the Send Signup Notification Email box.
If I do not tick Send Signup Notification Email then no email gets sent at all notifying of update.
I have made no change in your pdb update notify php file except replacing the email id in $from.
i want to be able for the update email to go automatically to the email id of the person whose record is being updated.
Sir , please advise.
thank you in advance and thank you for this plugin.
When the function in this plugin is called, it checks to see if there is an email address in the “email” field. Make sure that the email address you want the record to go to is in there, or maybe on your site it is in a different field. You will have to change the name of the field it is checking if that is the case…also of course, the name of the field in the $config array where the email address is provided to the email function.
This plugin also won’t send an email unless the record is getting updated in the admin section…an update on the frontend won’t trigger it.
This plugin is totally separate from the admin notification that is built into Participants Database, so don’t let that confuse the issue.
Turn Plugin Debugging on, clear the log, then test it. The email will be in the log if it is sent.
Hello Sir,
This is the plugin debug log. I noticed that both “to” and “message” info is not getting passed thru.
[12/02/22 4:46am UTC]
PDb_submission\main_query\base_query::execute_query storing record: UPDATE ii_participants_database SET
= “2022-12-02 10:16:25″,
= ‘S’,
= ‘ANAND’,
= ‘xxxx’,
= ‘xx@xx.com’,
= ‘No’,
= ‘test’,
= ‘abcde’,
= ‘abc’,
= ‘de’,
= ’11’,
= ” WHERE id = 230
[12/02/22 4:46am UTC]
xnau_Template_Email::_mail
context: record update notify
From: xx
Content-Type: text/html; charset=”UTF-8″
X-Generator: Participants Database
to: (is blank)
attachments: none
subj.: A record has just been updated
message: (is blank)
[12/02/22 4:46am UTC]
xnau_Template_Email::_mail sending failed for: while doing: record update notify
I had replaced $post[’email’] with $post[‘visitor_email’] as is the field info in my signup form and changed the text between ‘ ‘ in the subject and message fields.
function pdb_send_record_update_notification( $post )
{
if ( ! is_admin() || empty( $post[‘visitor_email’] ) ) {
thanks and regards.
SA
Hello, I am trying to send an email every time someone accesses a record, to edit it or to consult it, so that the record holder can know when someone has seen their data. What I don’t know is which hooks I should use.
Thank you very much and greetings.
The only user action you can hook to is when the user updates their record. There’s no hook that is triggered when they simply view the record. It is possible to write custom code that analyzes the shortcode request and triggers an email when certain conditions are met.
Hi, this shortcode not work:
*******************************************************************
add_shortcode(‘manda_correo’, ‘enviar_correo’);
function enviar_correo() {
if ( !is_admin() || !empty($post[’email’])) {
$subject = ‘Su página ha sido visitada.’;
$message = ‘Estimado [first_name] [last_name], su página de datos en CROHN & COLITIS QR TAG ha sido visitada, si no ha sido usted avise inmediatamente al administrador del sitio.’;
$from = ‘CROHN & COLITIS QR TAG ‘;
$config = array(
‘to’ => $post[’email’],
‘from’ => $from,
‘subject’ => $subject,
‘template’ => $message,
);
PDb_Template_Email::send( $config, $post );
}
}
**********************************************************
$post[’email’]) is NULL
Can you help my please?
Well, first, you need to familiarize yourself with the use of the add_shortcode function in WordPress. Then you have a problem, because you are attempting to access a variable that is not defined: $post has not been defined in the context of your function. You need to think about where you are using the shortcode and what data is available to the shortcode. Generally, the php global $_POST (which is perhaps what you’re looking for here) will not be available to the shortcode callback when it is placed in normal content. I don’t know where you intend to use the shortcode but you need to at least check to see if the $_POST data is available before trying to access it.
If you don’t understand the problem here, you may need to ask for the help of an experienced WordPress coder, I can’t provide support for custom code, I’m sorry.
Hello and happy holidays !!!
I have managed to make it work so that when you visit the page that contains [pdb_single] or the page that contains [pdb_record] an email is sent to the email that appears in that data, this serves as a security measure in case of unwanted access the data.
If anyone wants the code I will be happy to share it.
Happy New Year!!!
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
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.
Where is the PDB Email Templates Menu?
This is referring to the Email Expansion Kit premium add-on. Click the link for more info.
[…] 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 […]
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
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!
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.
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.
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.
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.
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'] ) ) {
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.
and to follow up on that, I cannot get it to work through the admin page either.
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.
I will check tonight to see if it is latest. Thanks for the help so far.
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;
}
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,
);
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.