In this tutorial i will explain how to schedule cron jobs with WordPress website. The cron is a time-based task/job scheduler which is running after a time intervals as per your settings during creating cron job in unix based operating systems. It is typically automates your several tasks/jobs.
Just assume if you need to export data from your database every hours, then you are doing it’s manually. In this case you can schedule cron jobs for doing this task automatically. First write your PHP script to export data from respective database and schedule cron job by using wp_schedule_event()
WordPress function.
wp_schedule_event()
WordPress action will trigger when one hour passed and executed your PHP script and export your data from database. In the below you snippets do_this_hourly()
function will execute your PHP code. Add below code at the end of the current activated theme’s functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | < ?php add_action('my_hourly_event', 'do_this_hourly'); function my_activation() { if ( !wp_next_scheduled( 'my_hourly_event' ) ) { wp_schedule_event(time(), 'hourly', 'my_hourly_event'); } } add_action('wp', 'my_activation'); function do_this_hourly() { // Add something to do every hours } ?> |
You ca also schedule your cron job using Cronjob Scheduler WordPress plugin. It is the best WordPress plugin to schedule your cronjobs in your WordPress websites. It will allow you to run your tasks timely and frequently without visit your website, all you need is at least 1 action and a Unix Crontab schedule!
Download the latest version of plugin from WordPress plugin directory and upload it in wp-content/pluigns folder after unzip the zip file. Now go the the Dashboard => Plugins and activate the Cronjob Scheduler plugin to enable it’s features. Below is the screenshots to configure the Cronjob Scheduler WordPress plugin.
Do you like & share this article with your friends, and don’t forget to follow us on Facebook and Twitter to learn cool WordPress tutorials.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: cron creator, cron job wordpress, cron jobs in wordpress, schedule event, wordpress beginners, wordpress beginners tutorial, wordpress cron, wordpress cron job, wordpress cron job plugin, wordpress schedule, wordpress tutorials for beginners, wp cron job