How to create cron job in WordPress

How to create cron job in WordPress

This post has been viewed 132 times.

In this tutorial, we would be learning how to create cron job in WordPress.

Sometimes you just need some automated functions in your WordPress plugins. Maybe you need to generate a report every 12 hours and you need to do this automatically, this is the function that you need to make this possible.

Did you know that wp-cron is different from the cron jobs offered by your hosting provider? Cron jobs in your hosting provider are more reliable than using wp-cron. Cron jobs execute exactly with the time you have set while in wp-cron you need someone to view your site for it to activate. But if you are at least having one (1) visit in your WordPress website then wp-cron should work.

What you’ll need:

  • A WordPress website.

Step 1: Setup the hook name for the action.

The hook name will be the name for your wp-cron. I’ll give you some example hook name just in case you don’t have any idea. If you are generating reports then you can name your hook name as generate_reports.

In this example we would just be using some generic hook name such as my_daily_event.

The use of register_activation_hook is when the plugin is activated it will add our wp-cron to the list of cron events.

In the wp_schedule_event we have used strtotime(“06:00:00”) and daily, this is in 24-hour format, which means our wp-cron will be executed every day at 06:00 AM.

On the first parameter, you can set any time you want and you can also use the time() function. The time() function is more reliable when creating a wp-cron that executes many times a day.

On the second parameter, you can pick from the default options from WordPress, you can select hourly (executes once per hour), twicedaily (executes every 12 hours), and daily (executes once a day). But if you want to add to add more option in these, I will show you how later.

Step 2: Create the function to be executed.

We will be creating the function that will be executed for our wp-cron.

In this case, our wp-cron will send a good morning email message to [email protected] every day at 06:00 AM.

Just put inside the function the codes you want to be executed automatically.

Step 3: Add a deactivation hook.

In Step 1 we have created an activation hook which executes when the plugin activates.

We need this to remove our hook when the plugin deactivates, which means it will stop from auto executing.

Step 4: Add a custom cron schedule.

Just incase you want to have a wp-cron that runs on a specific interval that is not available in the default options.

If we add the code above, it will add 5minutes and halfhour to the list of cron schedules. Meaning, at this point we already have five (5) cron schedules they are hourly, twicedaily, daily, 5minutes and halfhour. You can change the name to your preference and you can add more to the list.

Just take note that interval is expressed in seconds so we have to multiply the number of minutes by 60.

So if you are going to run a function every 5 minutes then you can simply just change daily to 5minutes.

Complete code:

Since this is a WordPress plugin, we must put this file in the plugin directory of our WordPress website.

There is a plugin that I recommend and use, it is named as WP Crontrol by John Blackbourn & contributors. It will show all the wp-cron events in detailed format which includes the hook name, the name of the function that executes, what time it executes and it even allows you to run a wp-cron manually. You can also add cron schedules without any coding.

That’s it, we have now created a wp-cron that executes automatically. I hope you have learned something.

Hello, I am a freelance website and mobile application developer. I am also the owner of this website, so if you have any projects for me you can contact me at [email protected] or use the contact page. When I’m not doing some work, I can be found watching Anime/Asian Dramas/American TV Series, playing computer games, and writing tutorials on this website. I run the program repeatedly hoping the error won’t show up next time.