Monday , 25 September 2017
Home / Programming / WordPress / Plugins / WordPress Cron Jobs – WordPress Plugin Tutorial
WordPress Cron Jobs
WordPress Cron Jobs

WordPress Cron Jobs – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

There will be times where you’ll want to continually perform certain tasks automatically. This can be anything from checking up on the server or sending out e-mails. Having these tasks run behind the scenes without anyone doing anything is known as cron jobs. Usually cron jobs are set up by the user on their web server, but now WordPress actually has a feature where you, the developer, can create cron jobs. WordPress cron jobs are easy to create and easy to manage.

Before you begin, you must be prepared to test this on a real server. While WordPress cron jobs don’t require that you use an actual server, we’ll be e-mailing ourselves and most local server environments don’t come with SMTP out of the box.

WordPress Cron Jobs

Create a new plugin called wpCronEx and update the index.php file to this.

We added 1 line of code to our wp_check_WP_ver() function. We’re introduced to a new function called wp_schedule_event(). It has 4 parameters. The first first parameter accepts when will this cron job first perform. You must insert a unix based timestamp value inside this. The time() function returns such a value. In our case, we want this cron job to perform when the plugin is first activated. The next parameter needs to be how often you want to run this cron job.  By default, WordPress creates 3 reoccurence values which are hourly, twicedaily and daily. We can create a custom interval, but more on that later.

The third parameter is the name of the hook that will allow for you to hook into this event. In our case, this would be jk_cron_hook_example. The great thing is that we can have multiple things hook into this action. The last parameter accepts an array of arguments to be passed into our hook functions. We won’t be passing any arguments, so we don’t need to insert anything for this.

We next create an action hook that hooks into our cron job event. Let’s create our function jk_update_message in the func.inc.php file.

This function is very simple, we’re simply e-mailing ourselves. I’m using my own e-mail. Please user your own e-mail for this example. The wp_mail() function is very similar to the mail() php function. The first 3 parameters are required. The first accepts the e-mail of who you’re sending this to. The seconds accepts the subject and the third accepts the body of the message.

That’s it! There’s one thing to add. WordPress cron jobs aren’t precise. Normal cron jobs are performed at the exact time of their assigned schedule. WordPress cron jobs are performed only when the interval has passed and when someone visits your WordPress site. So, let’s say you had a cron job performed hourly. If the last time the cron job was performed was an hour ago and no one visits your site for 3 hours, then your cron job won’t perform UNTIL someone eventually visits it. The schedule would then pick up again and wait for the next hour. So, WordPress cron jobs aren’t perfect yet.

Creating Custom Intervals

What if we wanted our WordPress cron jobs to perform every 30 minutes, 4 hours or even 15 seconds? It’s rather to easy to add your own custom interval. Let’s go back to our index.php code and under our hooks, add this line of code.

We’re hooking into a filter named cron_schedules. This is where cron job schedules are created. Let’s create our jk_add_cron_schedule() function in our func.inc.php file.

We are passed an array that contains all our cron job intervals. We create a new interval named thirty-sec. This name is what we’ll be referring to when we want to use this interval. The value must be an array. So, we’re pretty much creating multidimensional arrays. In our array, we must give 2 values. The length of time between each cron job in seconds. Then, we give a user friendly, human readable name for this cron job interval. We then return this array. Remember, this is a filter hook.

Now, let’s update our wp_schedule_event() function to this. Instead of inserting daily, we use the name we used for our key in the array. If you haven’t already figured it out, this cron job will perform every 30  seconds.

Display WordPress Cron Jobs

For whatever reason, you may want to display all the WordPress cron jobs. To do this is quite simple. Add this line of code under our hook section.

We’ll be displaying our WordPress cron jobs temporarily in the footer section.

Here’s our jk_display_cron_jobs() function.

The _get_cron_array() function returns an array of all WordPress cron jobs created. We then loop through each item in the array and echoing it out.

Deleting WordPress Cron Jobs

There will be times where you’ll want to delete a cron job. The most common case would be when someone deactivates your plugin. Under the set up section, add this line of code.

The register_deactivation_hook() function runs when the user deactivates the plugin. Just like the its opposite, it accepts 2 parameters. The first parameter accepts the path to the main plugin file. We pass in the __FILE__ magic constant. The next parameter accepts the function to run when the plugin is deactivated.

We first must grab the time of the next event occurring. We use the wp_next_scheduled() function to grab this time.  We need this time so we can cancel the event. We then use the wp_unschedule_event() function to unschedule the event. It has 2 parameters. The first parameter is the time of the event we want to cancel. We then pass in the cron job we want to cancel. This will stop the next and all future cron jobs you have scheduled for this certain cron job.

If you would like to learn more about cron jobs and see more examples, click here.

About Jasko Koyn

Check Also

WordPress Widgets API

Widgets API – WordPress Plugin Tutorial

The widgets API is what we’ll be learning next. It’s very simple to use. It …

2 comments

  1. Many thanks, nice tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link