wp_schedule_single_event 钩子没有发射
wp_schedule_single_event hook not firing
我为 Wordpress 中的管理 HR 系统安排了一个自定义事件。我需要为上传的文档安排到期警告,但我的挂钩没有触发。
function do_this_in_a_minunte($args) {
$to = "example@outlook.com";
$subject = 'New expiry email';
$body = "Hello " . $args;
$headers = array('From: ' . get_bloginfo ( 'name' ) . ' <' . get_bloginfo ( 'admin_email' ) . '>', 'Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
print_r($args);
}
add_action( 'my_new_event', 'do_this_in_a_minunte', 1, 2 );
$args = array(array('email' => 'test', 'title' => 'hello'));
wp_schedule_single_event( time() + (60 * 1), 'my_new_event', $args);
wp_schedule_single_event( time() + (60 * 2), 'my_new_event', $args);
wp_schedule_single_event( time() + (60 * 3), 'my_new_event', $args);
作业已正确添加到调度程序,但从未发送过任何电子邮件。该站点在此块之前发送调试电子邮件,所以我知道它可以发送电子邮件。
我无法在本地调试该站点,这很烦人,日志也没有显示任何有趣的内容。任何帮助表示赞赏。
我解决了,这很糟糕,但我要回答我自己的问题。
代码全部在 Elementor 自定义表单操作中。
public function run( $record, $ajax_handler ) {
function do_this_in_a_minunte($args) {
$to = "example@outlook.com";
$subject = 'New expiry email';
$body = "Hello " . $args;
$headers = array('From: ' . get_bloginfo ( 'name' ) . ' <' . get_bloginfo ( 'admin_email' ) . '>', 'Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
print_r($args);
}
add_action( 'my_new_event', 'do_this_in_a_minunte', 1, 2 );
$args = array(array('email' => 'test', 'title' => 'hello'));
wp_schedule_single_event( time() + (60 * 1), 'my_new_event', $args);
wp_schedule_single_event( time() + (60 * 2), 'my_new_event', $args);
wp_schedule_single_event( time() + (60 * 3), 'my_new_event', $args);
}
Wordpress 的“cron 任务”不会 运行,除非有人访问该站点,所以我将代码移到了 class 方法之外并移到了 functions.php 文件中,所以当作业 运行s 当有人访问站点并且队列得到处理时发送电子邮件的挂钩实际存在并且可以是 运行.
add_action( 'one_day_before_event', 'some_function', 10, 2 );
function some_function($post_id, $user_id) {
error_log("some_function firing");
$to = "test@outlook.com";
$subject = 'TEST';
$body = $post_id . " - " . $user_id;
$headers = array('From: ' . get_bloginfo ( 'name' ) . ' <' . get_bloginfo ( 'admin_email' ) . '>', 'Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
}
答案是确保你的钩子存在于你正在设置任务的 class 方法之外,否则当它需要 运行.
时它不存在
我为 Wordpress 中的管理 HR 系统安排了一个自定义事件。我需要为上传的文档安排到期警告,但我的挂钩没有触发。
function do_this_in_a_minunte($args) {
$to = "example@outlook.com";
$subject = 'New expiry email';
$body = "Hello " . $args;
$headers = array('From: ' . get_bloginfo ( 'name' ) . ' <' . get_bloginfo ( 'admin_email' ) . '>', 'Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
print_r($args);
}
add_action( 'my_new_event', 'do_this_in_a_minunte', 1, 2 );
$args = array(array('email' => 'test', 'title' => 'hello'));
wp_schedule_single_event( time() + (60 * 1), 'my_new_event', $args);
wp_schedule_single_event( time() + (60 * 2), 'my_new_event', $args);
wp_schedule_single_event( time() + (60 * 3), 'my_new_event', $args);
作业已正确添加到调度程序,但从未发送过任何电子邮件。该站点在此块之前发送调试电子邮件,所以我知道它可以发送电子邮件。
我无法在本地调试该站点,这很烦人,日志也没有显示任何有趣的内容。任何帮助表示赞赏。
我解决了,这很糟糕,但我要回答我自己的问题。
代码全部在 Elementor 自定义表单操作中。
public function run( $record, $ajax_handler ) {
function do_this_in_a_minunte($args) {
$to = "example@outlook.com";
$subject = 'New expiry email';
$body = "Hello " . $args;
$headers = array('From: ' . get_bloginfo ( 'name' ) . ' <' . get_bloginfo ( 'admin_email' ) . '>', 'Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
print_r($args);
}
add_action( 'my_new_event', 'do_this_in_a_minunte', 1, 2 );
$args = array(array('email' => 'test', 'title' => 'hello'));
wp_schedule_single_event( time() + (60 * 1), 'my_new_event', $args);
wp_schedule_single_event( time() + (60 * 2), 'my_new_event', $args);
wp_schedule_single_event( time() + (60 * 3), 'my_new_event', $args);
}
Wordpress 的“cron 任务”不会 运行,除非有人访问该站点,所以我将代码移到了 class 方法之外并移到了 functions.php 文件中,所以当作业 运行s 当有人访问站点并且队列得到处理时发送电子邮件的挂钩实际存在并且可以是 运行.
add_action( 'one_day_before_event', 'some_function', 10, 2 );
function some_function($post_id, $user_id) {
error_log("some_function firing");
$to = "test@outlook.com";
$subject = 'TEST';
$body = $post_id . " - " . $user_id;
$headers = array('From: ' . get_bloginfo ( 'name' ) . ' <' . get_bloginfo ( 'admin_email' ) . '>', 'Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
}
答案是确保你的钩子存在于你正在设置任务的 class 方法之外,否则当它需要 运行.
时它不存在