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!


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.

  • 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

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'));

I think you forgot register_activation_hook()

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

  • 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
  • I don't think you need to register an activation hook, if you add it, it's registered automatically. – Jim Maguire Oct 15 '15 at 7:27

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.