WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can I use wp_schedule_event() to set up a cron job from within a widget?

I tried, but I can't make it work, The problem seems to be the $hook parameter. The function I'm trying to hook is located within the widget class so my guess is that WP can't find it.

neither of these work:

wp_schedule_event(time(), 'daily', 'my_widget_cron');
wp_schedule_event(time(), 'daily', array(&$this, 'my_widget_cron'));

any ideas?

share|improve this question
up vote 4 down vote accepted

wp_schedule_event takes a hook as parameter, not a function. Try:

wp_schedule_event(time(), 'daily', 'my_daily_event');

add_action('my_daily_event', array(&$this, 'my_widget_cron'));

if ( !wp_next_scheduled( 'my_daily_event' ) ) {
    wp_schedule_event(time(), 'hourly', 'my_daily_event')

If you remove the widget from the sidebar, the cron will still continue to run. You can run the following code (outside of the widget class) to clear it:

if ( !is_active_widget('your_widget_callback_function') && wp_next_scheduled( 'my_daily_event' ) ) {
share|improve this answer
thanks! now it works :) I have one more question: If I remove the widget from the sidebar, will the cron still continue to run? – Alex Nov 1 '10 at 19:21
Yes, it will continue to run. You will have to clear it. I updated my answer with a sample code. – sorich87 Nov 1 '10 at 20:06

Your Answer


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.