0

So, I'm developing a plugin, I wanted to know if I call

$timestamp = wp_next_scheduled( 'scheduled_hook' );
wp_unschedule_event( $timestamp, 'scheduled_hook' );

Without checking if it's scheduling at all, will it give me an error or will it just go through it and unschedule it in case it is actually scheduled and ignore it otherwise?

3
  • 3
    Sounds like you could easily test this, no? From the function signature, it seems that wp_unschedule_event() returns WP_Error on failure, so I would assume that this is what happens.
    – kero
    Commented Oct 21, 2021 at 15:40
  • I actually could test it, but since I searched for the answer online and couldn't find, I decided to ask, so if anyone (like i did), searched for it, they could find the answer instead of having to test it (And I could do some other stuff while testing it wasn't really critical!) Commented Oct 21, 2021 at 17:47
  • Valid reasons. I didn't mean to be harsh with my message - feel free to self-answer the question once you know the answer!
    – kero
    Commented Oct 22, 2021 at 7:15

1 Answer 1

1

So yeah, it does give errors for trying to unschedule something that isn't scheduled, but the answer is pretty easy and simple:

$timestamp = wp_next_scheduled( 'scheduled_hook' );
if ($timestamp) wp_unschedule_event( $timestamp, 'scheduled_hook' );

Since wp_next_scheduled will return false for unscheduled hooks, a simple if in front of the call to wp_unschedule_event will prevent it from being called it there's nothing scheduled

Your Answer

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

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