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 |1 Answer
Reset to default 1So 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
本文标签:
版权声明:本文标题:plugin development - Will I get an error if I try unscheduling a WP Cron scheduled task that wasn't scheduled? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741248240a2365300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_unschedule_event()
returnsWP_Error
on failure, so I would assume that this is what happens. – kero Commented Oct 21, 2021 at 15:40