A cron job lets you run a bit of the code at a specific interval. For example, you could have a script that can remove the website trash with an hourly interval. To set up a cron job isn’t something you do on your lunch break. Luckily, you can do this in WordPress with the wp_schedule_event()
function.
Note: You can schedule a hook that’ll be executed by the WordPress actions core on a specific interval specified by you. The action will trigger when someone visits your website, if the scheduled time has passed.
Here’s an example to set up a cron job on hourly interval.
Instructions: Add the following code in the functions.php file of your WordPress theme to see it in action. You can change the time as you need.
<?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() { // do something every hour } ?>
Make sure to replace the function do_this_hourly()
with the task you want to achieve with this cron job.
You may also enjoy flushing permalinks once per hour cron job.
Comments Leave a Reply