wp_schedule_single_event()计划某个钩子(hook)的使用时间

目录

描述

译文

计划某个钩子(hook)的使用时间,用户指定时间后,WordPress动作(action)函数执行一次该钩子。预定时间过去后,如果有人访问用户的WordPress网站,动作函数会停止运行。

原文

Schedules a hook which will be executed once by the WordPress actions core at a time which you specify. The action will fire off when someone visits your WordPress site, if the schedule time has passed.

wp_schedule_single_event() 描述

用法

 <?php wp_schedule_single_event$timestamp$hook$args ); ?> 

Note that scheduling an event to occur before 10 minutes after an existing event of the same name will be ignored, unless you pass unique values for $args to each scheduled event. See wp_next_scheduled() for more information.

This behavior is subject to change, as the original intention of the code was to prevent scheduling two identical events within ten minutes of each other, not preventing scheduling of identical events until ten minutes after the next scheduled occurrence.

Attempts to schedule an event after an event of the same name and $args will also be ignored.

wp_schedule_single_event() 用法

参数

$timestamp

(integer) (必填) The time you want the event to occur. This must be in a UNIX timestamp format. WP cron uses UTC/GMT time, not local time. Use time(), which is always GMT in WordPress. (current_time( 'timestamp' ) is local time in WordPress.)

默认值: None

$hook

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

默认值: None

$args

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

默认值: array()

wp_schedule_single_event() 参数

示例

Schedule an event one hour from now

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

function do_this_in_an_hour() {

    // do something
}
add_action( 'my_new_event','do_this_in_an_hour' );

// put this line inside a function, 
// presumably in response to something the user does
// otherwise it will schedule a new event on every page visit

wp_schedule_single_event( time() + 3600, 'my_new_event' );

// time() + 3600 = one hour from now.

Schedule an event one hour from now with arguments

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

function do_this_in_an_hour( $arg1, $arg2, $arg3 ) {
    // do something
}
add_action( 'my_new_event', 'do_this_in_an_hour', 10, 3 );

// put this line inside a function, 
// presumably in response to something the user does
// otherwise it will schedule a new event on every page visit

wp_schedule_single_event( time() + 3600, 'my_new_event', array( $arg1, $arg2, $arg3 ) );

// time() + 3600 = one hour from now.

wp_schedule_single_event() 示例

注意

历史

添加于 版本: 2.1.0

wp_schedule_single_event() 历史

源文件

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Schedules a hook to run only once.
 *
 * Schedules a hook which will be executed once by the WordPress actions core at
 * a time which you specify. The action will fire off when someone visits your
 * WordPress site, if the schedule time has passed.
 *
 * @since 2.1.0
 * @link https://codex.wordpress.org/Function_Reference/wp_schedule_single_event
 *
 * @param int $timestamp Timestamp for when to run the event.
 * @param string $hook Action hook to execute when cron is run.
 * @param array $args Optional. Arguments to pass to the hook's callback function.
 * @return void|false
 */
function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
	// don't schedule a duplicate if there's already an identical event due within 10 minutes of it
	$next = wp_next_scheduled($hook, $args);
	if ( $next && abs( $next - $timestamp ) <= 10="" *="" minute_in_seconds="" )="" {="" return;="" }="" $crons="_get_cron_array();" $event="(object)" array(="" 'hook'=""> $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
	/**
	 * Filter a single event before it is scheduled.
	 *
	 * @since 3.1.0
	 *
	 * @param object $event An object containing an event's data.
	 */
	$event = apply_filters( 'schedule_event', $event );

	// A plugin disallowed this event
	if ( ! $event )
		return false;

	$key = md5(serialize($event->args));

	$crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
	uksort( $crons, "strnatcasecmp" );
	_set_cron_array( $crons );
}

wp_schedule_single_event() 源文件

相关