wp_unschedule_hook

函数
wp_unschedule_hook ( $hook, $wp_error = false )
参数
  • (string) $hook Action hook, the execution of which will be unscheduled.
    Required:
  • (bool) $wp_error Optional. Whether to return a WP_Error on failure. Default false.
    Required:
    Default: false
返回值
  • (int|false|WP_Error) On success an integer indicating number of events unscheduled (0 indicates no events were registered on the hook), false or WP_Error if unscheduling fails.
定义位置
相关方法
wp_clear_scheduled_hookwp_unschedule_eventwp_schedule_eventwp_next_scheduledwp_get_schedule
引入
4.9.0
弃用
-

wp_unschedule_hook: 这个函数取消与指定的行动钩子相关的所有事件的日程安排。动作钩子是事件的名称,为该钩子安排的事件可以用这个函数取消计划。

取消连接到钩子的所有事件的时间表。

在停用插件以清理cron队列时,可能会很有用。

警告: 这个函数可能会返回布尔值FALSE,但也可能会返回一个非布尔值,该值会被评估为FALSE。关于向布尔值转换的信息请参见{@link PHP documentation}。使用`===`操作符来测试此函数的返回值。

function wp_unschedule_hook( $hook, $wp_error = false ) {
	/**
	 * Filter to preflight or hijack clearing all events attached to the hook.
	 *
	 * Returning a non-null value will short-circuit the normal unscheduling
	 * process, causing the function to return the filtered value instead.
	 *
	 * For plugins replacing wp-cron, return the number of events successfully
	 * unscheduled (zero if no events were registered with the hook) or false
	 * if unscheduling one or more events fails.
	 *
	 * @since 5.1.0
	 * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
	 *
	 * @param null|int|false|WP_Error $pre      Value to return instead. Default null to continue unscheduling the hook.
	 * @param string                  $hook     Action hook, the execution of which will be unscheduled.
	 * @param bool                    $wp_error Whether to return a WP_Error on failure.
	 */
	$pre = apply_filters( 'pre_unschedule_hook', null, $hook, $wp_error );

	if ( null !== $pre ) {
		if ( $wp_error && false === $pre ) {
			return new WP_Error(
				'pre_unschedule_hook_false',
				__( 'A plugin prevented the hook from being cleared.' )
			);
		}

		if ( ! $wp_error && is_wp_error( $pre ) ) {
			return false;
		}

		return $pre;
	}

	$crons = _get_cron_array();
	if ( empty( $crons ) ) {
		return 0;
	}

	$results = array();
	foreach ( $crons as $timestamp => $args ) {
		if ( ! empty( $crons[ $timestamp ][ $hook ] ) ) {
			$results[] = count( $crons[ $timestamp ][ $hook ] );
		}
		unset( $crons[ $timestamp ][ $hook ] );

		if ( empty( $crons[ $timestamp ] ) ) {
			unset( $crons[ $timestamp ] );
		}
	}

	/*
	 * If the results are empty (zero events to unschedule), no attempt
	 * to update the cron array is required.
	 */
	if ( empty( $results ) ) {
		return 0;
	}

	$set = _set_cron_array( $crons, $wp_error );

	if ( true === $set ) {
		return array_sum( $results );
	}

	return $set;
}

常见问题

FAQs
查看更多 >