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.

This is a MU-plugin I'm developing, and I want it to run hourly. Unfortunately, it's running every time I visit any site's wp-admin page. I've used the Debug Bar plugin with the Cron plugin, and it consistently says that the next event is in the past. Any ideas?

add_action('admin_menu', 'dhg_cron_menu');
function dhg_cron_menu()
{
    add_options_page('NEW WPMS Stats', 'NEW WPMS Stats', 'manage-options', 'dhg-cron', 'dhg_cron_settings');
}

function dhg_cron_settings()
{
    if(!wp_next_scheduled('dhg_cron_hook'))
    {
        wp_schedule_event(time(), 'hourly', 'dhg_cron_hook');
    }
}

add_action('dhg_cron_hook', 'dhg_cron_get_stats');
function dhg_cron_get_stats()
{
    //  this is a custom function I use to log to a file for testing...
    log_wpms_stats("testing!");
}
share|improve this question
1  
WordPress Codex recommends scheduling an event during plugin activation, see: codex.wordpress.org/Function_Reference/… Have you already tried that? Because there is no need to run that on every page load. Could be an easy solution. –  swissspidy Nov 7 '13 at 18:20
1  
I copied and pasted your code and worked form me. And cron debug bar correctly display next schedule in 59 minutes after first run... –  G. M. Nov 7 '13 at 18:49

1 Answer 1

An idea I came up with was using the new WP Heartbeat API as opposed to using cron. Have your plugin fire off a call to your logging system every hour.

Pippin's tutorial contains the majority of the code you'll need to get started.

share|improve this answer

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.