嗨,我使用这个插件创建事件https://wordpress.org/plugins/modern-events-calendar-lite/,这是发送通知在我的安卓应用程序。https://github.com/dream-space/wp-fcm
我做了修改插件发送提醒通知即将到来的事件(3天前)。修改看起来就像。
`
function fcm_main_get_option_event(){
$options = get_option('fcm_event_setting');
if(!is_array($options)){
$options = array(
'event_check' => 0,
);
}
return $options;
}
if (!wp_next_scheduled('my_task_hook')) {
wp_schedule_event( time(), 'daily', 'my_task_hook' );
}
add_action ( 'my_task_hook', 'my_task_function' );`
以及发送通知的功能:
`
function my_task_function() {
$options = get_option('fcm_event_setting');
// $is_send_notif = false;
if(!empty($options['event_check'])) {
$is_send_notif = true;
$args = array(
'post_type' => 'mec-events',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$datetime2 = date("Y-m-d");
$my_meta = get_post_meta(get_the_ID(), 'mec_start_date', true );
$diff = strtotime($my_meta) - strtotime($datetime2);
$gap = abs(round($diff / 86400));
if($gap == '3'){
$is_send_notif = true;
$title = 'Потсетување';
$event = "POTSETNIK 1";
$body = 'For '.$gap.' days :' .get_the_title();
}
if($is_send_notif == true){
$message = array(
'title' => $title,
'content' => 'For '.$gap.' days:' .get_the_title()
);
$total = fcm_data_get_all_count();
if($total <= 0) return;
$respon = fcm_notif_divide_send("", $total, $message);
fcm_data_insert_log($title, $content, "ALL", $event, $respon['status']);
}
endwhile;
wp_reset_postdata();
}
}`
这项工作,但发送通知前3天,和3天后的事件。
问题在哪里。
谢谢?
发布于 2021-03-18 10:11:37
这是因为您的代码是显式编写的,以忽略事件是过去的还是将来的。
见这里:
$diff = strtotime($my_meta) - strtotime($datetime2);如果事件日期是未来的,$diff将是一个正数,但如果它是过去的,它将是一个负数。接下来的一行是:
$gap = abs(round($diff / 86400));abs()函数的全部目的是确保一个数字是一个正数。这意味着无论事件是过去的还是将来的,结果都是完全一样的。
如果您只想为即将到来的事件发送通知,那么您需要知道该数字是否为负数。因此,只需删除abs(),并确保只检查3而不是-3:
$diff = strtotime($my_meta) - strtotime($datetime2);
$gap = round($diff / 86400);
if($gap === 3){
// etc.
}https://wordpress.stackexchange.com/questions/385281
复制相似问题