Scheduling and Monitoring WordPress Action Hooks


January 26th, 2010 0 comments

WordPress has integrated a pretty good solution for generating scheduled tasks, such as publishing scheduled posts or activating the backup script. By default it’s executed when a visitor arrives on your website. The system checks if a scheduled task needs executing or not.

This only works if you have a popular site. If your visitor count is low some actions might not be generated at the right time. For this is a simple solution – using the Unix system feature crontab. For an explanation about using it with WordPress check out my article WordPress Cron – How To Use Iit The Right Way?.

But enough of the introduction, lets move forward with using hooks and adding our own custom schedules tasks.

What are WordPress action hooks?

I couldn’t quite put it into words to explain it myself so I found a perfect quote for it instead:

Action hooks are essentially placeholders. Wherever an action hook is placed, it will execute any code that has been “hooked” to it.

by Nathan Rice

Nathan explains very clearly what these things really are and what they are used for inside WordPress.

How can I create a custom action hook?

Recently I was experimenting with displaying the number of RSS subscribers and Twitter followers on this blog. For this I wanted to create an automated script, what inserted the count in the database to use less requests during website loading. This is why I started looking into using hooks and adding my own script to the scheduled tasks list.

All of these custom tasks should be put in your functions.php file, what is placed in your theme folder. So lets open up the file and start coding.

First we need to create the function that needs executing (this is the place all the needed tasks are done).

function my_custom_function() {
    wp_mail('email@example.com', 'Scheduled email', 'Our scheduled email works!');
}

my_custom_function is the name of the function and all this function does is send out an email to email@example.com. Just a simple example to show how this thing works.

Now that we have our function in place we need to add the hook what executes the function.

add_action('my_custom_hook', 'my_custom_function');

my_custom_hook is the name of the hook, my_custom_function is the name of our function what be put together earlier.

By now we have our function and the hook that does the executing.

How do I add my custom hook to the cron and how can I monitor it?

For this I found the best way is to use the plugin WP-Crontrol. You may ask me why use a plugin for this if it can be done via a simple line of code added to the functions.php file? The reason is not only using it for adding scheduled tasks to the system but also monitoring them.

Crontrol

With WP-Crontrol you can add scheduled tasks, edit and delete them using a nice interface. You can even create your own intervals that fit your needs. It also gives you a list of all the entries in the cron system.

Aside these great features it also has a “Do now” feature, with what you can execute the hook outside the schedule. What is it useful for? A simple example.

Lets say you’ve set up a backup script that creates a database backup every three hours. A new version of WordPress is released and you want to create a fresh database backup (something that should always be done before upgrading!). With this plugin you can just hit the “Do now” button and after that upgrade your WordPress to the latest version.

I hope this little explanation was helpful for creating your custom scheduled tasks.

Now what?

After reading this post maybe you'd be interested in reading some related posts?
You can also help me spread the word or leave a comment.
If you're planning on leaving maybe you'd like to subscribe via RSS Feed
or follow me on Twitter.

Spread the word

Leave a reply