WordPress Cron Jobs
Cron Jobs is intended to run scheduled tasks on web servers. WordPress has a built-in Cron Jobs system that allows you to publish scheduled posts, for example. In this tutorial you will learn how Cron Jobs works and how to view and use it in WordPress.
What is WordPress Cron Jobs and how does it work?
Cron Jobs is used to execute tasks at specific times or at regular intervals. From web servers, the system is used to maintain the server and execute scheduled tasks.
WordPress has its own built-in Cron Jobs system that can be used for checking for updates, for example, and it can also be used by plugins. For example, if you want to update every two weeks with your backup plugin, Cron Jobs can do it for you. However, irresponsible use of the system can slow down your website, in which case you need to find and fix the problem.
How to view and control the WordPress Cron Jobs System
Step 1: Install WP Crontrol
First, you need to install and activate the plugin WP Control on your website.
Step 2: View Cron Jobs
After activating the plugin, go to Tools –> Cron Events to view all Cron Jobs and control the settings.
After clicking on “Cron Events” you will see a list of all cron jobs that should be run on your website. In the first column you will see the name of the hook that runs the cron job, this hook usually gives a hint of what the event does.
Most default WordPress hooks start with a wp_ prefix, for example wp_update_themes. Plugins sometimes use their own prefix.
You can also see when the cron job should be run and if you hover over one cron job, you can edit, run now or delete it.
Step 3: Edit a Cron Job
To edit a cron job (for example, to change how often it should be executed, to make it less resource intensive), simply click on “Edit” after hovering over the cron job you want to change. This will open the “Edit Cron Event” tab, there you can change the following:
When you are done with your changes, click on “Update Event” to save everything.
How to add your own Cron Job in WordPress
Step 2: Add your Information
Now you need to add a hook name for the cron job (they can’t contain special characters or spaces). The next field is the optional arguments field, this is for the executed function, if it requires arguments, but them in this field. Then you need to specify when the cron job should be executed. You can select “now”, “tomorrow” or a date and time. Then you need to select when the cron job should be repeated; you can also select that it should not be run again.
When you finished adding the information, click on “Add Event” to save everything. The new cron job is then displayed in the list.
Step 3: Add a Function
The cron job is not running yet because WordPress has not been told what to do when the event is triggered. You will need to create a hook and a function that runs when the cron job hook is triggered.
This example sends a test email to the specified email address when the cron job is executed.
add_action( 'ad_custom_cron', 'ad_custom_cron_func' ); function ad_custom_cron_func() { wp_mail( 'info@arrowdesign.ie', 'Automatic email', 'Automatic scheduled email from WordPress to test cron'); }
If you now click on “Run Now” for the cron job, it will be executed.
Conclusion
As you have seen in this post, the WordPress Cron Jobs System can be very helpful and is easy to use. We hope this post was helpful! Here you can find more information related to this topic:
Read another WordPress post : ‘Correctly enqueue javascript and css in WordPress‘
Another great article about Cron Jobs can be found here.
Read more about the WP Control plugin here.
Arrow Design, based in Dublin, Ireland, provides quality website design services in Dublin and beyond at affordable prices. If you would like help with implementing the above code, or any wordpress website development project, contact us. We love website design and it shows! We provide custom wordpress plugin development, website design training and lots more.
We do it all, so you don’t have to!
…We do more, so you can do less 🙂