wp_cron() not updating time from options page

enter image description here

I’m working in a simple plugin that initiates a wp_cron job.

I have an options page with a field (daily_message_time) where I enter my desired time to schedule the cron, say 17:30:00 for example, and it gets stored in the _options table as expected. I can then get that value and use it in my plugin script like this:

register_activation_hook(__FILE__, 'send_message');
function send_message() {
    if (! wp_next_scheduled ( 'daily_email_event' )) {
        $options        = get_option( 'my_settings' );
        $daily_time     = $options['daily_message_time'];

        // Honour WordPress timezone setting
        $date = new DateTime( $daily_time, new DateTimeZone( get_option( 'timezone_string' ) ) );
        $timestamp = $date->getTimestamp();

        // Shedule the event
        wp_schedule_event($timestamp, 'daily', 'daily_email_event');

add_action('daily_email_event', 'send_email');
function send_email() {
    // Do stuff

This works and I can see my cron has been scheduled for the correct time:

But the problem is this: If I change the time in my options page, the new time does get stored in the database but the cron job does not show the new time (it keeps the original time). The only way I can get it to work is by deactivating and reactivating the plugin again, after which it then shows the new time.

How can I get around this?

Read more here: wp_cron() not updating time from options page

Leave a Reply

Your email address will not be published. Required fields are marked *