4

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!");
}
  • 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... – gmazzap Nov 7 '13 at 18:49
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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