0

I have a function that I've been attempting to get to work with wp_schedule_event. The function sends a notification to the admin. I see the event get added to the wp cron event list, but it never runs the function.

Function as follows:

<?php

function send_booking_expiration_notification(){
  // do woocomerce booking stuff... then send email.
 wp_mail($to, $subject, $message, $headers);
}

// Schedule the send_booking_expiration_notification() function to run daily at midnight
if ( ! wp_next_scheduled( 'send_booking_expiration_hook' ) ) {
    wp_schedule_event( strtotime( '12pm' ), 'hourly', 'send_booking_expiration_hook' );
}
add_action( 'send_booking_expiration_hook', 'send_booking_expiration_notification' );

?>

Am I misunderstanding scope or hooks. I've added the add_action above the function and still no dice. I can call the function and the have confirmed the function works and send email, but something with the cron isn't working.

3
  • Note... I know this is set to hourly, did this for testing. But this will be reverted back to daily and send a notification at 7am EST daily.
    – Cody
    Dec 15, 2022 at 16:29
  • it's very possible that it is running but either crashing, or the email is being discarded once it leaves the server ( especially if you're on a shared server or sending lots of emails ), note that generally it's not a great idea to run code in the root of a file that isn't in a hook unless it's a theme template outputting HTML, Vancoders answer may well be the solution here
    – Tom J Nowell
    Dec 15, 2022 at 16:48
  • this function is within the functions.php of the theme directory.
    – Cody
    Dec 15, 2022 at 17:08

1 Answer 1

1

You're probably attempting to add this too early. Wait for init.

add_action(
    'init',
    function () {
        if ( ! wp_next_scheduled( 'send_booking_expiration_hook' ) ) {
            wp_schedule_event( strtotime( '12pm' ), 'hourly', 'send_booking_expiration_hook' );
        }
    }
);
4
  • I've added this into the themes functions.php does this still need to wait for init. I have also confirmed this is added to the event list using wp cli i.e the wp cron event list. | send_booking_expiration_hook | 2022-12-15 17:00:00 | 47 minutes 43 seconds | 1 hour |
    – Cody
    Dec 15, 2022 at 17:06
  • the time passes and never get anything
    – Cody
    Dec 15, 2022 at 17:07
  • how are you testing it? And how are you confirming that it ran? If you tested the code as is you would need to wait until 12pm noon, so you couldn't possibly have checked this in the 38 minutes since the answer was written
    – Tom J Nowell
    Dec 15, 2022 at 17:19
  • A good trick I use whenever I implement scheduled events is that I record the time the CRON last ran: $current_time = date( 'F j, Y, g:i a' ); to capture the time, then update_option( 'sendbooking_last', $current_time ); With that, you can always do a search of your wp_options table for sendbooking_last and see whether or not the value is updating every hour. Dec 15, 2022 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.