admin管理员组文章数量:1122846
I'm working on a plugin to register and schedule various cron jobs. I am:
- using WP Crontrol (just to see all of my cron jobs and override them if need be)
- have a server cron job set up and have
define('DISABLE_WP_CRON', true);
in place
When I went into 'Cron Events' within Crontrol I saw that some functions were added literally hundreds of times. Specifically, in 'Cron Events' the function is what's getting registered/listed, not the hook. A simplified version of my code:
class My_Cron_Manager {
/**
* Constructor
*/
public function __construct() {
add_filter( 'wp', array( $this, 'my_schedule_cron_jobs' ) );
add_action( 'my_cron_event_func', array( $this, 'my_cron_job_func' ));
}
/**
* Schedule cron Jobs
*/
public static function my_schedule_cron_jobs() {
if (! wp_next_scheduled ( 'my_cron_job_func' )) {
wp_schedule_event(time(), 'twicedaily', 'my_cron_job_func');
};
}
/**
* My Function
*/
public static function my_cron_job_func() {
// Do Stuff
}
}
new My_Cron_Manager();
I have a suspicion that I had an error in my code that was re-registering the function, but I'm not entirely sure. Am I utilizing wp_next_scheduled
and wp_schedule_event
correctly?
Can someone please let me know if my above code would be causing duplicated hook/function registration?
Thanks in advance!
I'm working on a plugin to register and schedule various cron jobs. I am:
- using WP Crontrol (just to see all of my cron jobs and override them if need be)
- have a server cron job set up and have
define('DISABLE_WP_CRON', true);
in place
When I went into 'Cron Events' within Crontrol I saw that some functions were added literally hundreds of times. Specifically, in 'Cron Events' the function is what's getting registered/listed, not the hook. A simplified version of my code:
class My_Cron_Manager {
/**
* Constructor
*/
public function __construct() {
add_filter( 'wp', array( $this, 'my_schedule_cron_jobs' ) );
add_action( 'my_cron_event_func', array( $this, 'my_cron_job_func' ));
}
/**
* Schedule cron Jobs
*/
public static function my_schedule_cron_jobs() {
if (! wp_next_scheduled ( 'my_cron_job_func' )) {
wp_schedule_event(time(), 'twicedaily', 'my_cron_job_func');
};
}
/**
* My Function
*/
public static function my_cron_job_func() {
// Do Stuff
}
}
new My_Cron_Manager();
I have a suspicion that I had an error in my code that was re-registering the function, but I'm not entirely sure. Am I utilizing wp_next_scheduled
and wp_schedule_event
correctly?
Can someone please let me know if my above code would be causing duplicated hook/function registration?
Thanks in advance!
Share Improve this question edited Aug 7, 2017 at 19:20 Ryan Dorn asked Aug 7, 2017 at 19:08 Ryan DornRyan Dorn 3499 silver badges23 bronze badges 2 |1 Answer
Reset to default 0Ok, well I believe that I have things working correctly. I'd be curious if anyone has any more insights, but I believe what was happening:
- I was calling my class incorrectly
- I was referencing a class function in
wp_next_scheduled
andwp_schedule_event
when I should have been referencing a callback/hook.
My revised code (which has been tested and I can confirm works).
define( 'MY_CRON_FILE', __FILE__ );
class MY_Cron_Manager {
/**
* Hook in methods.
*/
public static function init() {
register_activation_hook( MY_CRON_FILE, array(__CLASS__, 'my_schedule_cron_jobs' ));
add_action( 'my_cron_job_event_func', array( __CLASS__, 'my_cron_job_func' )); //
}
/**
* Schedule cron Jobs
*/
public static function my_schedule_cron_jobs() {
if (! wp_next_scheduled ( 'my_cron_job_event_func' )) {
wp_schedule_event(time(), 'twicedaily', 'my_cron_job_event_func');
};
}
/**
* My Function
*/
public static function my_cron_job_func() {
// Do Stuff
}
}
MY_Cron_Manager::init();
本文标签: hooksDuplicate Cron Jobs Using wpnextscheduledwpscheduleevent
版权声明:本文标题:hooks - Duplicate Cron Jobs Using wp_next_scheduledwp_schedule_event 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736293678a1929195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
new My_Cron_Manager();
at the end of the file, but where and how is this called? Alsowp
is an action hook, I believe, not a filter (semantics, I know). But where is the action hookmy_cron_event_func
and how is that called? – hwl Commented Aug 7, 2017 at 19:20