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
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?

share|improve this question
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 '12 at 20:33

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.

share|improve this answer

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.

share|improve this answer
Did the html comment appear in ur header? This is a plugin btw. – Zhianc Jan 26 '12 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. – Rob Vermeer Jan 26 '12 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.

share|improve this answer

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.