Admin settings update updating every time home page is hit?

A much better hook to use would the the update_option_{option_name} dynamic hook, which only runs after your specific option has been updated. So something like:

add_action('update_option_woocommerce_email_send_time', 'send_time')

function update_send_time() {
     // Add code here to update cron job
}

The only problem you’ll get here is that scheduling the cron job for a time earlier than the current time will trigger it to send right away. For example, if it is 3:40pm right now, and you schedule the email to be sent at 1pm, it will sent right away because that time has already passed today. If you scheduled it for 5pm, it would wait until 5pm. Make sense?

What I’d probably do is store another value (probably using WordPress Transients) with the last date the summary email was sent, and then double check that value before sending the email out. Once the email is sent, I’d update that transient with the current date.

References:

update_option function – https://developer.wordpress.org/reference/functions/update_option/

Leave a Comment