WordPress cron aka schedule?

register_activation_hook(__FILE__, 'cron_post_activation');
add_action('post_event', 'cron_post');
function cron_post_activation() {
    wp_schedule_event(time(), 'hourly', 'post_event');
}

function cron_post() {
 //Do stuff
}

This is not working by default – found out the hard way!

After debugging for hours I stumbled on several posts which claimed that you need to add next line to wp-config.php:

Read More
define('DISABLE_WP_CRON', true);

1) What’s the logic behind that? Disabling something in order it to work doesn’t make sense to me.

2) How to set it up with your own intervals? Docs states you can only use ‘hourly’, ‘twicedaily’ and ‘daily’.

Related posts

1 comment

  1. Yes, you do need to disable WP_CRON, however, you also need to set up your own cron job outside of WordPress to trigger the event. By default, WP’s cron feature only kicks in when someone views the site, and even then it’s not guaranteed that it will run exactly on time. It’s not useful at all if you need something to happen at a specific time or interval.

    Assuming you’re on a *nix server, the best explanation I have found on how to do this is:

    http://bitswapping.com/2010/10/using-cron-to-trigger-wp-cron-php/

    The gist of it is that you turn off WordPress’s way of triggering its own scheduler, and then access that system directly on a schedule that you set up using the cron program on your server. What this does is set up a barebones WP environment in which to execute tasks without sending any HTML back. This can result in much better performance than pinging your site homepage directly if you have the job set up to visit every minute.

    And to the point about “turning it off to make it work” – yes, that seems counterintuitive. What you are actually turning off is the system that checks whether or not to execute the cron task or not. By turning that off, you essentially are building a direct pipeline to that system and telling it to do the job every single time, whenever you tell it to. You’re taking the variance out of the system.

Comments are closed.