4

I have a widget

class Xwidget extends WP_Widget{
function cron_addB(){}

}

I want to run a cron job that calls that function from outside the class

function xxx_cron_activation() {   ;
    if ( !wp_next_scheduled( 'xxx_followers' ) ) {
    wp_schedule_event(time(), 'hourly', 'xxx_followers');
}
}
add_action('wp', 'xxx_cron_activation');

add_action('xxx_followers',array('Xwidget',"cron_addB"));

Cron is being triggered but the function is not being executed

1 Answer 1

2

Try this instead:

$gloabl $myxclass;
$myxclass = new Xwidget();
add_action( 'xxx_followers', array(&$myxclass, "cron_addB" ) );

or

add_action('xxx_followers', "init_xclass_and_cron");
function init_xclass_and_cron() {
    $myxclass = new Xwidget();
    $myxclass->cron_addB();
}
5
  • that won't work; the $myxclass object won't be initialized inside the cron request... Commented Jul 14, 2011 at 17:10
  • How about setting $myxclass globally?
    – Scott
    Commented Jul 14, 2011 at 17:15
  • see update for another method
    – Scott
    Commented Jul 14, 2011 at 17:19
  • 1
    The 2nd one should work. The functions that cron executes are stored as simple strings in the db, so they should be static. The OP's code should also work if the method is really static... Another way is add_action('xxx_followers', 'Xwidget::cron_addB'); Commented Jul 14, 2011 at 19:19
  • the second one works like charm! Thanx guys
    – Freeman
    Commented Jul 17, 2011 at 1:00

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.