WP_cron won’t trigger my action

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:

Read More
  • 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.

Related posts

Leave a Reply

2 comments

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