Earlier I wrote a few articles on creating the expertise plugin and how to add shortcodes to it. Today I want to add scheduled events to the plugin. When somebody posts a question to the expertise page, we could probably write a trigger that checks immediately who’s the expert for the section and send a notice in an email. However, I think it’s also nice to illustrate making a timed even that will check all expertise pages at once for new messages and send out all notifications right away.
Activation Code
One thing we have not yet added to the plugin is a decent activation/deactivation section. We should do this first, we’ll see why later on. In WordPress, a plugin can be activated and deactivated through the administrative plugin menu. These events can be trapped with the register_activation_hook() and register_deactivation_hook() functions. Typically these hooks get used to create extra database tables needed by a plugin, set default values for the plugin etc.
Another thing that can be done in an activation/deactivation section for a plugin is to create / remove a timed event for the plugin:
register_activation_hook(__FILE__, 'expert_activate');
add_action ('expert_msgcheck', 'hourly_msgcheck');
register_deactivation_hook(__FILE__, 'expert_deactivate');
function expert_activate () {
wp_schedule_event(time(), 'hourly', 'expert_msgcheck');
}
function expert_deactivate () {
wp_clear_scheduled_hook('expert_msgcheck');
}
function hourly_msgcheck () {
}
The above code snippet sets this all up; The add_action() call at first seems kind of pointless here as there’s no point in time where the WordPress core will execute this action. This all changed when an administrator chooses to activate the Expertise plugin. Through the register_activation_hook function expert_activate() will be called. This function in turn calls wp_schedule_event() which schedules the action expert_msgcheck (and thus the function hourly_msgcheck()) to be executed hourly, starting immediately.
Work with Me
Let’s add some code to the hourly_msgcheck() function. We know that we want it to send email eventually, so to check if the mechanism works we’ll simply let the function send us an email every hour. On my setup I need to use an SMTP mailer as I’m using Google Apps for email. This requires some settings we might as well initialize on activation of the plugin:
function expert_activate () {
wp_schedule_event(time(), 'hourly', 'expert_msgcheck');
add_option('expert_settings', array(
'onpost' => 1,
'smtp_user' => '',
'smtp_password' => '',
'smtp_server' => 'smtp.gmail.com'));
}
function expert_deactivate () {
wp_clear_scheduled_hook('expert_msgcheck');
unregister_setting('experts-settings-group', 'expert_settings');
delete_option('expert_settings');
}
I’ve changed my earlier approach here from a variable expert_onpost to adding all related options into an array. Just learning as I go, and reading over Handling Plugins Options in WordPress 2.8 with register_setting() many times to see if I could figure out what’s going on there.
Remember we used the new API’s register_setting() call in a previous article? Well that’s still compatible with add_option() so it’s safe to set default values for the options with this. I have included the deactivation code though, just to show that you still need to use delete_option() as unregister_setting() does not take care of this for you.
I also added a parameter validation function:
register_setting( 'experts-settings-group', 'expert_settings','expert_onpost_validate');
function expert_onpost_validate ($input) {
// Our first value is either 0 or 1
$input['onpost'] = ($input['onpost'] == 1 ? 1 : 0);
return $input;
}
This simply checks whether the variable onpost is actually a boolean. I haven’t included any validation checks on the other variables yet.
So with the registered variables in place it becomes really very easy to do the actual form building / handling:
function expert_settings_page() {
?>
<div>
<h2>Experts</h2>
<form method="post" action="options.php">
<?php settings_fields( 'experts-settings-group' ); ?>
<?php $expert_settings = get_option("expert_settings"); ?>
<table>
<tr valign="top">
<td scope="row">On Post in Expertise Section</td>
<td>
<input name="expert_settings[onpost]" type="radio" value="0" <? checked(0,$expert_settings['onpost']); ?> /> Do Nothing <input name="expert_settings[onpost]" type="radio" value="1" <?php checked(1,$expert_settings['onpost']); ?> /> Send Email
</td>
</tr>
<tr>
<td>SMTP username</td>
<td>
<input name="expert_settings[smtp_user]" type="text" value="<?php echo $expert_settings['smtp_user']; ?>" />
</td>
</tr>
<tr>
<td>SMTP password</td>
<td>
<input name="expert_settings[smtp_password]" type="password" value="<?php echo $expert_settings['smtp_password']; ?>" />
</td>
</tr>
<tr>
<td>SMTP server</td>
<td>
<input name="expert_settings[smtp_server]" type="text" value="<?php echo $expert_settings['smtp_server']; ?>" />
</td>
</tr>
</table>
<p>
<input type="submit" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php } ?>
Notice that there is no form handling in the plugin itself anymore! That’s all done by WordPress itself.
So now that we can actually set some required options, let’s add this send_email() function:
function send_email () {
// PHP Mailer Variables
if (!class_exists("phpmailer")) {
require_once(ABSPATH.'wp-includes/class-phpmailer.php');
}
$expert_settings = get_option("expert_settings");
$mail = new PHPMailer();
$mail->Mailer = "smtp";
$mail->Username = $expert_settings['smtp_user'];
$mail->Password = $expert_settings['smtp_password'];
$mail->Host = $expert_settings['smtp_server']
$mail->SMTPSecurity = "tls";
$mail->Post = 465;
$mail->SMTPAuth = true;
$mail->CharSet = "utf-8";
$mail->Encoding = "quoted-printable";
$mail->FromName = "3DN Techblog <3dn@3dn.nl>";
$mail->AddReplyTo = "Fred Leeflang <fredl@3dn.nl>";
$mail->Subject = "Test Email";
$mail->Body = "Testing gmail functionality";
$mail->AddAddress("fredl@dutchie.org");
if ($mail->Send()) {
return 1;
} else {
return 0;
}
}
And add a call to this function in the hourly_msgcheck() function just to see if it works:
function hourly_msgcheck () {
send_email();
}
Read More
If you find this article interesting you might want to read some more articles in the WordPress category:
- Playing with Simple Facebook Connect
- New 3DN Techblog
- Add Ajax to Expertise Plugin
- Checking Comments for Requests
- Adding sendmail/SMTP to experts plugin
Or in the Coding category:
- Playing with Simple Facebook Connect
- Facebook Bookmarks for Chrome?
- Add Ajax to Expertise Plugin
- Checking Comments for Requests
- Adding sendmail/SMTP to experts plugin
If you have any questions regarding WordPress, check out our Wordpress page or our Programming page. Note: The code I’m writing about is constantly under development. I can’t write about every little change I make but I try. I’m learning as I go because this is my first WordPress plugin. If you’re interested in the latest code you can always look in SVN through my public Trac server or simply download the latest development copy.