1. 首页
  2. WordPress 函数手册

wp_clear_scheduled_hook()

wp_clear_scheduled_hook( string $hook, array $args = array() )

使用指定参数取消附加到钩子的所有事件。
Unschedules all events attached to the hook with the specified arguments.

目录锚点:#说明#参数#源码#笔记


说明(Description)

警告:此函数可能返回布尔值FALSE,但也可能返回计算结果为FALSE的非布尔值。有关强制转换为布尔值的信息,请参阅PHP文档。使用===运算符测试此函数的返回值。


参数(Parameters)

参数 类型 说明
$hook (string) 动作钩子,其执行将是非计划的。
$args (array) 要传递给钩子的回调函数的参数。

源码(Source)

/**
 * Unschedule all cron jobs attached to a specific hook.
 *
 * @since 2.1.0
 *
 * @param string $hook Action hook, the execution of which will be unscheduled.
 * @param array $args Optional. Arguments that were to be pass to the hook's callback function.
 */
function wp_clear_scheduled_hook( $hook, $args = array() ) {
	// Backward compatibility
	// Previously this function took the arguments as discrete vars rather than an array like the rest of the API
	if ( !is_array($args) ) {
		_deprecated_argument( __FUNCTION__, '3.0', __('This argument has changed to an array to match the behavior of the other cron functions.') );
		$args = array_slice( func_get_args(), 1 );
	}

	// This logic duplicates wp_next_scheduled()
	// It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
	// and, wp_next_scheduled() returns the same schedule in an infinite loop.
	$crons = _get_cron_array();
	if ( empty( $crons ) )
		return;

	$key = md5( serialize( $args ) );
	foreach ( $crons as $timestamp => $cron ) {
		if ( isset( $cron[ $hook ][ $key ] ) ) {
			wp_unschedule_event( $timestamp, $hook, $args );
		}
	}
}
更新版本 源码位置 使用 被使用
5.1.0 wp-includes/cron.php 12 18

笔记(Notes)

基本示例

wp_clear_scheduled_hook() 为WP2原创文章,链接:https://www.wp2.cn/functions/wp_clear_scheduled_hook/