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

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();
}
  • that won't work; the $myxclass object won't be initialized inside the cron request... – onetrickpony Jul 14 '11 at 17:10
  • How about setting $myxclass globally? – Brady Jul 14 '11 at 17:15
  • see update for another method – Brady Jul 14 '11 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'); – onetrickpony Jul 14 '11 at 19:19
  • the second one works like charm! Thanx guys – Freeman Jul 17 '11 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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