scheduled event not getting executed

class checkPost{
  function __construct()
  {
    if ( !wp_next_scheduled('my_task_hook') ) {
      wp_schedule_event( time(), 'hourly', 'my_task_hook' ); // hourly, daily and twicedaily
    }
    add_action('my_task_hook', array(&$this,'my_task_function'));
  }
  function my_task_function() 
  {
    add_action('wp_head', array(&$this,'wp_head'));
  }
  function wp_head()
  {
    echo '<!--This is a test!-->';
  }
}

That’s the stripped code of my plugin, but it’s somehow not working. Why is it not?

3 Answers
3

You can’t add an action to wp_head inside a wp-cron job. The wp-cron tasks run outside of the normal flow, so while you can add your action, it won’t actually do anything to anybody viewing the page. Cron jobs are called via separate http requests, no user normally will see their results.

In other words, your code works fine, it just doesn’t do what you’re expecting it to do. Although to be honest, I’m not entirely sure what you were expecting it to do. You certainly won’t see any change to the content of the page header, if that’s what you were thinking.

Leave a Comment