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