Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have one function: "update_user_like" to sum ​​all facebook likes from all user's posts.

How to run that function at 3am every day by wp cron job?

Thanks much!

share|improve this question
    
set the cron at 3am to repeat every 24 hours. –  JMau Jun 20 '13 at 17:09
    
I don't know how to write that code before :( Can you help me write down with a complete code? Thank you very much! –  Zen Nguyễn Jun 20 '13 at 17:12
add comment

1 Answer 1

First of all you have to remember how WP cron works. The action will trigger when someone visits your WordPress site, if the scheduled time has passed. So if your site isn't very crowded at 3am, the cron job won't start exactly at 3am. (You should use real cron if you really need to run it exactly at 3am).

To schedule an event at 3am every day you can use this code:

function do_this_daily_after_3am() {
    // do something every day after 3am
}
add_action('my_daily_event', 'do_this_daily_after_3am');


function my_activation() {
    if ( !wp_next_scheduled( 'my_daily_event' ) ) {
        $next3am = ( date('Hi') >= '0300' ) ? strtotime('+1day 3am') : strtotime('3am');  // you can calculate it in any way you want

        wp_schedule_single_event( $next3am, 'my_daily_event');
    }
}
add_action('wp', 'my_activation');
share|improve this answer
    
Thanks you! I will try it and tell you the the results its works. –  Zen Nguyễn Jun 21 '13 at 2:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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