Skip to content

How To Schedule Cron Jobs In WordPress

A cron job let’s you automatically run a function at any specific interval. A cron job manager comes out with your hosting package, but there’s no need of it if you’re using WordPress. WordPress has its built-in cron, which is all we need.

You can easily schedule cron jobs in WordPress with the wp_schedule_event() function. Just add the following snippet to your current theme’s functions.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
     }

The action will trigger when someone visits your WordPress site, if the scheduled time has passed. Our function will execute the cron job every hour. You can change hourly in the above code with twicedaily or daily.

Leave a Reply

Your email address will not be published. Required fields are marked *