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?

  • wp-cron.php seems to have some issues since 3.3; I found several threads of people complaining. My own posts miss schedule since then, too. No idea.
    – Raphael
    Jan 26, 2012 at 20:33

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.


I think it is working. I pasted your code in my functions.php and called the class. Then with a cron viewer I see the cron job

Jan 26, 2012 @ 21:25 (1327613109) Once Hourly my_task_hook

Also tested it out by adding an usermeta field to my account via the cron function and that also worked.

  • Did the html comment appear in ur header? This is a plugin btw.
    – Zhianc
    Jan 26, 2012 at 20:44
  • Nope it doesn't. It seems like you can't use add_action inside a cron function... Don't know that for sure though. Jan 26, 2012 at 21:13

Calling wp_head from within a cron process seems to be odd. What exactly are you aiming for. Did you debug with error_log to ensure that your cron job is really not fired off.

If your cron jobs don't fire you might find some help with wp-cron-control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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