2

I have a WordPress function that I want to run once daily at a specific time. How should I go about doing this, since WP cron cannot be set to a particular time?

2
  • See if this can help Codex Commented Dec 13, 2013 at 7:02
  • 1
    How specific time? You can create WP cron events for specific time, it just won't be precisely executed.
    – Rarst
    Commented Dec 13, 2013 at 10:15

3 Answers 3

7

you can absolutely use wp_cron to specify a time:

add_action( 'my_scheduled_event', 'prefix_my_scheduled_event' );
/**
 * On the scheduled action hook, run a function.
 */
function prefix_my_scheduled_event() {
    // do something 
}
//going to use the strtotime function, so a good habit to get into is to set the PHP timezone to UTC
default_timezone_set( 'UTC' );
//define a timestamp to run this the first time.  4:20 is as good a time as any.
$timestamp = strtotime( '2013-12-14 16:20:00' ); 
//set a recurrence interval - WP has three default intervals: hourly, daily & twicedaily
$recurrence = 'daily';
//define the hook to run
$hook = 'my_scheduled_event'
//you can pass arguments to the hooked function if need be.  this parameter is optional:
$args = null;
//the following will run your function starting at the time defined by timestamp and recurring every $recurrence
wp_schedule_event( $timestamp, $recurrence, $hook, $args );

This is an over simplified example. Some type of checking needs to be done so that you don't end up with a boatload of hooks scheduled, but it should give you an idea of how to set it up for a particular time. Further, it needs to be understood that wp_cron is triggered by page loads so if your site hasn't got huge amounts of traffic, then the function won't fire precisely at the time defined. There are workarounds for this though.

2
  • Thanks that's what I was looking for. What kind of checking are you referring to though? I thought WP handles that on its own.
    – urok93
    Commented Dec 13, 2013 at 23:12
  • in the above example, the wp_schedule_event function will be called whenever the php file that it lives in is loaded. It should be wrapped in some sort of conditional statement. The example in the documentation looks something like this: if ( ! wp_next_scheduled( 'my_scheduled_event' ) ) { wp_schedule_event( $timestamp, $recurrence, $hook, ); } note that if you passed any arguments to wp_schedule_event that the same arguments need to be passed to wp_next_scheduled as the second parameter
    – Will
    Commented Dec 14, 2013 at 4:11
0

You can go through this link.

http://codex.wordpress.org/Function_Reference/wp_schedule_event

2
  • 3
    Please explain how the link is related to the Question, how he/she can implement your solution - explain your answer in details. Providing just a link is not a good answer. Commented Dec 13, 2013 at 8:33
  • As per above answer given by "Will the Web Mechanic" go to solution Commented Dec 14, 2013 at 5:35
0

Just to add to @Will the Web Mechanic's answer. I needed to use

date_default_timezone_set()

default_timezone_set didn't work for me

2
  • Welcome to WPSE Seán! Thanks for adding your advice. It would be better as a comment on Will's answer, but I realise you need reputation to add comments, so I've flagged this in the hopes that a moderator can convert it to a comment for you.
    – Tim Malone
    Commented Apr 17, 2016 at 22:00
  • thanks, i couldn't comment earlier due to being a new user, great if it can get moved
    – John Behan
    Commented Apr 18, 2016 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.