Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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();
}
share|improve this answer
    
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

 
discard

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.