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'm in a dead end with a schedualed task in a wordpress plugin for a multisite. Somehow the action I added don't get triggered. The task is getting schedualed and returns a timestamp when I run wp_next_scheduled(), but the action itself doesn't go off and trigger the function.

Information that might give some clues:

  • It's running on a WPMU-site
  • The cronjob is a part of a plugin
  • I'm using OOP approach
  • The things I'm importing is a custom post type.

Code Example:

class Cronjobs
{
    function Cronjobs()
    {
        add_action('init', array(&$this, 'add_cronjobs'));
    }

    function add_cronjobs()
    {
        add_action('update_properties_daily', array(&$this, 'do_updates'));

        if(!wp_next_scheduled('update_properties_daily') )
        {
           wp_schedule_event( time(), 'daily', 'update_properties_daily');
        }
    }

    function do_updates()
    {
        /* Do updates */
    }
}

Could really use the help of some wizes on this one, thanks!


UPDATE

Solution: Create a global function and call that from the custom action.

Apparently is there some glitch with creating custom actions while refering to an object. Since custom actions is stored in the DB (as far as I've understood), the objected won't be instansiated and therefor can't use it's methods. Therefor: back to basics and use a global function.

share|improve this question

migrated from stackoverflow.com Sep 28 '11 at 10:45

This question came from our site for professional and enthusiast programmers.

    
Are you 100% sure it's not getting called? Try logging something to file as the first line of your action - it may be that it is being called but causing a fatal error before anything useful happens. –  DaveRandom Sep 26 '11 at 11:16
    
Has a file_put_contents() in the first line, that works when the method is called outside the action. No success, though. Thx for the answer, btw. –  user534736 Sep 26 '11 at 11:34
    
Where / when do you instantiate your class? Ensure the action is registered and the class instantiated when wpcron gets fired. –  hakre Sep 26 '11 at 11:35

2 Answers 2

You need to call add_action from outside the class, with a reference to the object. Example:

$cj = new Cronjobs;
add_action('update_properties_daily', array(&$cj, 'do_updates'));
share|improve this answer

I think you forgot register_activation_hook()

See http://codex.wordpress.org/Function_Reference/wp_schedule_event

share|improve this answer
    
Is that really required? Since I'm running it on init and conditioned the wp_schedule_event to only run only if the job doesn't exists I'd figured it would be working anyway. Hmm. No can't be it. It does return a timestamp, so the shedualed task exists. It seems like its the action that isn't working. –  user534736 Sep 26 '11 at 11:27

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.