Citizen SP
Citizen SP

Reputation: 1411

Wordpress wp_schedule_event at specific time

I'm using the following functions to activate/deactivate and run a cronjob in Wordpress every hour.

I already checked the Wordpress codes and visited tutorial sites, but don't see an example to run wp-cron at a specific time, like '23.59am every day'. Is this possible?

register_activation_hook(__FILE__, 'cron_activation');

add_action('twitter_cron', 'cron_function');

register_deactivation_hook(__FILE__, 'cron_deactivation'); 

function cron_activation() {
wp_schedule_event(time(), 'hourly', 'twitter_cron');
}

function my_deactivation() {
    wp_clear_scheduled_hook('twitter_cron');
}

function do_this_hourly() {

// my twitter function

if (!wp_next_scheduled('twitter_cron')) {
wp_schedule_event( time(), 'hourly', 'twitter_cron' );
}

Upvotes: 0

Views: 4740

Answers (2)

Jacques
Jacques

Reputation: 38

In wp_schedule_event($timestamp, $recurrence, $hook, $args); the first param is the current time. WP find the last event in DB (WP_OPTIONS line CRON), look at the interval and if $timestamp is after, execute the cron.

To run the cron at a specific time : set $recurrence more little (18 hours for a daily) and stop the cron before the specific time with a test

$hcron = 3; // 03:00 AM
$t = time();
$on = mktime($hcron,0,0,date("m",$t+(6-$hcron)*3600),date("d",$t+(6-$hcron)*3600),date("Y",$t+(6-$hcron)*3600));
if ($t>=$on) { add_action('cron'...)}

Upvotes: 0

Citizen SP
Citizen SP

Reputation: 1411

I found the solution:

wp_schedule_event('1339631940', 'daily', 'my_wp_cron' );  

Where '1339631940' is the linux timestamp for Wed, 13 Jun 2012 23:59:00 +0000.

Upvotes: 1

Related Questions