admin管理员组

文章数量:1277875

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?

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?

Share Improve this question asked Oct 21, 2021 at 15:29 Miguel VieiraMiguel Vieira 134 bronze badges 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!) – Miguel Vieira 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
Add a comment  | 

1 Answer 1

Reset to default 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

本文标签: