$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>cron - Wp_Schedule_Event every day at specific time|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

cron - Wp_Schedule_Event every day at specific time

matteradmin9PV0评论

I write Following code to run my function every day at 16:20 ..

But I guess is the problem in code..

not working

Epoch timestamp: 1427488800

3/28/2015 1:10:00 AM

if( !wp_next_scheduled( 'import_into_db' ) ) {
wp_schedule_event( time('1427488800'), 'daily', 'import_into_db' );

function import_into_db(){

////My code

}
add_action('wp', 'import_into_db');
}

I write Following code to run my function every day at 16:20 ..

But I guess is the problem in code..

not working

Epoch timestamp: 1427488800

3/28/2015 1:10:00 AM

if( !wp_next_scheduled( 'import_into_db' ) ) {
wp_schedule_event( time('1427488800'), 'daily', 'import_into_db' );

function import_into_db(){

////My code

}
add_action('wp', 'import_into_db');
}
Share Improve this question edited Feb 28, 2015 at 7:46 Mortzea asked Feb 27, 2015 at 21:24 MortzeaMortzea 1962 gold badges3 silver badges13 bronze badges 1
  • 1 The time() function doesn't take an input argument, try the strtotime() function or the strftime() function instead, to create custom timestamps from a string. But if you already got the timestamp, you don't need them. – birgire Commented Feb 27, 2015 at 23:06
Add a comment  | 

4 Answers 4

Reset to default 31

WP Cron runs, when somebody visits your website. Thus if nobody visits, the cron never runs.

Now there are 2 solutions:

  1. Disable WP Cron, use a real cron job and customize it.

https://support.hostgator/articles/specialized-help/technical/wordpress/how-to-replace-wordpress-cron-with-a-real-cron-job

  1. Use a custom interval in wp_schedule_event():

    function myprefix_custom_cron_schedule( $schedules ) {
        $schedules['every_six_hours'] = array(
            'interval' => 21600, // Every 6 hours
            'display'  => __( 'Every 6 hours' ),
        );
        return $schedules;
    }
    add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );
    
    //Schedule an action if it's not already scheduled
    if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
        wp_schedule_event( time(), 'every_six_hours', 'myprefix_cron_hook' );
    }
    
    ///Hook into that action that'll fire every six hours
     add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );
    
    //create your function, that runs on cron
    function myprefix_cron_function() {
        //your function...
    }
    

and you can see these tuts

http://www.nextscripts/tutorials/wp-cron-scheduling-tasks-in-wordpress/

http://www.iceablethemes/optimize-wordpress-replace-wp_cron-real-cron-job/

http://www.smashingmagazine/2013/10/16/schedule-events-using-wordpress-cron/

custom Wp cron

http://codex.wordpress/Plugin_API/Filter_Reference/cron_schedules

http://www.smashingmagazine/2013/10/16/schedule-events-using-wordpress-cron/

http://www.viper007bond/2011/12/14/how-to-create-custom-wordpress-cron-intervals/

http://www.sitepoint/mastering-wordpress-cron/

https://tommcfarlin/wordpress-cron-jobs/

http://www.paulund.co.uk/create-cron-jobs-in-wordpress

cron linux

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

http://www.thesitewizard/general/set-cron-job.shtml

http://code.tutsplus/tutorials/scheduling-tasks-with-cron-jobs--net-8800

google search

Instead of time(), use strtotime() function so you can specify the time of day - it will use today's date with the time that you specify. So in your case:

strtotime('16:20:00'); // 4:20 PM

Usage in the wp_schedule_event function would look like this:

wp_schedule_event( strtotime('16:20:00'), 'daily', 'import_into_db' );

Well, 1427488800 resolves to March 27, 2015, so your event isn't even set to start yet.

Also, bear in mind that scheduled WP events will only fire if someone hits the site at that time.

This code is working for me, I believe it is closer to the original question. You want to get the UNIX time + timezone for when you want it to execute. Once the cron executes and is removed from WP, it recreates itself at the time you specify.

In the following example, I have it working for AEST (GMT+10) @ 6AM each morning. So I am scheduling it for GMT 20:00 each day.

if (!wp_next_scheduled('cron_name')) {
    $time = strtotime('today'); //returns today midnight
    $time = $time + 72000; //add an offset for the time of day you want, keeping in mind this is in GMT.
    wp_schedule_event($time, 'daily', 'cron_name');
}
Post a comment

comment list (0)

  1. No comments so far