The Participants Database plugin includes a handy class for sending template-based emails. You can easily use this class to send custom emails from the plugin.
The way this is usually done is to use the “send” static method of the class. You will need to provide the method with the email configuration information and a data array for use in the template. In this post, I will explain how that is done.
The Email Template
If you are familiar with the Participants Database plugin, you will have seen the template the plugin uses for sending emails. These templates are found in the plugin settings where the content of the emails are defined. The template can include “placeholder tags” that tell the email function which data field to replace the tag with.
To demonstrate how this works, if you had a line in your template like this:
Welcome, [[first_name]] [[last_name]]!
The Template Email class would use the matching values from the data array to replace the tags in the template. So, if your data array contained:
$data = array( 'first_name' => 'Roland', 'last_name' => 'Barker');
In the email, it would become:
Welcome, Roland Barker!
You aren’t constrained to the fields in the Participants Database, you can use any tags you want to invent, just include the value in the $data array.
Configuring the Email
To send an email, you will need certain values such as who the email is going to and and who it’s from. These details are provided to the class in a $config array, which looks like this:
$config = array( 'to' => $user_email, 'from' => 'Your Website <firstname.lastname@example.org>', 'subject' => '[first_name], You Have Been Approved!', 'template' => 'Welcome, [first_name] [last_name]!' );
This is how we tell the class where to send the email, who it’s from, the subject, and the body of the email. The body is usually supplied as a template, that is why it is labeled ‘template.’
You’ll also notice that the subject contains a placeholder tag. The “subject” config item will have any placeholder tags replaced by the matching data.
The “from” element is optional: if you don’t include it, the plugin settings for the “from” name and address will be used.
This is fed to the PDb_Template_Email::send() method to configure the email you want to send. When you use this method, you will change the values in that configuration array to suit your needs.
Using the PDb_Template_Email::send() Method
We use the send() method to both configure and send the email in one action. You can call this method anywhere in your WP installation…so, in your functions.php file, or in any plugin you might have…anywhere. This code snippet shows how to do that:
// define the data to use $data = array( 'first_name' => 'Roland', 'last_name' => 'Barker'); // set up email configuration values $config = array( 'to' => $user_email, 'from' => 'Your Website <email@example.com>', 'subject' => '[first_name], You Have Been Approved!', 'template' => 'Welcome, [first_name] [last_name]!' ); // now, send the email PDb_Template_Email::send($config, $data);
The email is sent as soon as the send method is called.
That’s really all there is to it. Once you understand how it works, you can easily add any custom emails you need.
For a practical application of this class, read this tutorial: