wp_clear_scheduled_hook()清除某预订的定时事件的钩子(hook)

目录

描述

Un-schedules all previously-scheduled cron jobs using a particular hook name or a specific combination of hook name and arguments.

wp_clear_scheduled_hook() 描述

用法

 <?php wp_clear_scheduled_hook$hook$args ); ?> 

wp_clear_scheduled_hook() 用法

参数

$hook

(string) (必填) The name of an action hook to execute.

默认值: None

$args

(array) (可选) Arguments to pass to the hook function(s)

默认值: None

wp_clear_scheduled_hook() 参数

示例

Clear a scheduled event

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

// If you previously added for example
// wp_schedule_single_event( time() + 3600, 'my_new_event' );

wp_clear_scheduled_hook( 'my_new_event' );

// or this if you created something like
// wp_schedule_single_event( time() + 3600, 'my_new_event', array( 'some_arg' ) );

wp_clear_scheduled_hook( 'my_new_event', array( 'some_arg' ) );

wp_clear_scheduled_hook() 示例

注意

If you created a scheduled job using a hook and arguments you cannot delete it by supplying only the hook. Similarly if you created a set of scheduled jobs that share a hook but have different arguments you cannot delete them using only the hook name, you have to delete them all individually using the hook name and arguments.

wp_clear_scheduled_hook() 注意

历史

添加于 版本: 2.1.0

wp_clear_scheduled_hook() 历史

源文件

wp_clear_scheduled_hook() 函数的代码位于 wp-includes/cron.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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 );
		}
	}
}

wp_clear_scheduled_hook() 源文件

相关