admin管理员组

文章数量:1206198

I created a custom plugin and I need to schedule add_action using Crontrol Plugin, the Setup class is from different php class. But the schedule action cant read the Crontrol even if I execute do_action('schedule') inside functions.php it is not working.

class Scheduler {
    
    public $setup;

    public function __construct()
    {

        add_action('scheduler', [$this, 'etf_scheduler_func']);
    }

    public function etf_scheduler_func() {
        $this->setup = new Setup();
        $this->setup->set_table();
    }
}

I created a custom plugin and I need to schedule add_action using Crontrol Plugin, the Setup class is from different php class. But the schedule action cant read the Crontrol even if I execute do_action('schedule') inside functions.php it is not working.

class Scheduler {
    
    public $setup;

    public function __construct()
    {

        add_action('scheduler', [$this, 'etf_scheduler_func']);
    }

    public function etf_scheduler_func() {
        $this->setup = new Setup();
        $this->setup->set_table();
    }
}
Share Improve this question edited Mar 21, 2022 at 7:00 JuniorA asked Mar 21, 2022 at 6:40 JuniorAJuniorA 11 bronze badge 2
  • You haven't scheduled anything. That's what the Crontrol plugin shows; scheduled actions. See this for how to schedule things in WordPress: developer.wordpress.org/plugins/cron – Jacob Peattie Commented Mar 21, 2022 at 8:09
  • Thank you for your suggestion, I posted my answer. – JuniorA Commented Mar 21, 2022 at 13:54
Add a comment  | 

1 Answer 1

Reset to default -1

I didn't call the class yet, this code is now working

class Scheduler {
    
    public $setup;
    public static $instance;

    public function __construct()
    {
        self::$instance =& $this;

        add_action('scheduler', [$this, 'etf_scheduler_func']);
    }

    public function etf_scheduler_func() {
        $this->setup = new Setup();
        $this->setup->set_table();
    }

    public static function &get_instance()
    {
        if ( ! isset( self::$instance ) )
            self::$instance = new self;

        return self::$instance;
    }
} 

Scheduler::get_instance();

本文标签: phpHow to execute addaction() function from custom plugin to Crontrol plugin or doaction()