In WordPress you can setup scheduled tasks that allow you to run a certain action at a scheduled time in the day. As this runs a set action you can perform a number of tasks at the same time. To setup the scheduled tasks all you have to do is use the WordPress function of wp_schedule_event(). The schedule event function takes 4 parameters.
<?php wp_schedule_event($timestamp, $recurrence, $hook, $args); ?>
- $timestamp - This must be the timestamp of the first time you want this task to run.
- $recurrence - How often you want this to run, hourly, daily, monthly, etc.
- $hook - The hook that you want to run on the scheduled event.
- $args - Any additional arguments that you want to send through to the hook.
Once these are setup, they will run forever until you turn them off, so make sure you are only performing tasks that you will continuously need to run. If the task that you need to perform is relatively resource heavy then you should make sure you perform these tasks during your website's less busy times. While in development what would happen if you face problems with the code you you wrote for the action, what if you tried using an undefined variable? Your script will break but you won't know about it because your script will run outside of the application and you will never see the error. There is a way that you can manually run all the scheduled tasks that you have setup in WordPress, just go to the URL http://www.example.com/wp-cron.php?doing\_cron. This will start all the scheduled tasks so you can see any errors that can happen in your script.
Turn Off WordPress Cron Jobs
If you want to turn off any cron jobs while you are debugging the system then you can do so by putting the following code in the wp-config.php.
Now you can manually call your cron jobs without them automatically running.