Niroj Adhikary
Niroj Adhikary

Reputation: 1835

How to prevent executing wp cron on every page visit?

I have scheduled a wp cron

$time = strtotime(date('Y-m-d'));
wp_schedule_event($time, 'daily', 'loyality_expiry_notification');
add_action('loyality_expiry_notification', 'send_notification');
function send_notification() {...}

send_notification sends email notification daily.But i am receiving email on every page visit. I want to execute this event only once in a day.

Upvotes: 1

Views: 45

Answers (1)

Niroj Adhikary
Niroj Adhikary

Reputation: 1835

!wp_next_scheduled did the job.

if ( ! wp_next_scheduled( 'loyality_expiry_notification' ) ) {
     wp_schedule_event($time, 'daily', 'loyality_expiry_notification');
}

Upvotes: 1

Related Questions