wp_schedule_event

函数
wp_schedule_event ( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false )
参数
  • (int) $timestamp Unix timestamp (UTC) for when to next run the event.
    Required:
  • (string) $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values.
    Required:
  • (string) $hook Action hook to execute when the event is run.
    Required:
  • (array) $args Optional. Array containing arguments to pass to the hook's callback function. Each value in the array is passed to the callback as an individual parameter. The array keys are ignored. Default empty array.
    Required:
    Default: array()
  • (bool) $wp_error Optional. Whether to return a WP_Error on failure. Default false.
    Required:
    Default: false
返回值
  • (bool|WP_Error) True if event successfully scheduled. False or WP_Error on failure.
定义位置
相关方法
wp_reschedule_eventwp_unschedule_eventwp_get_scheduled_eventwp_schedule_single_eventwp_scheduled_delete
引入
2.1.0
弃用
-

wp_schedule_event: 这是一个WordPress的函数,用来安排一个重复发生的事件。它允许你指定一个时间间隔和一个将在该间隔内执行的回调函数。例如,你可以用这个函数来安排一个脚本每小时运行一次来执行某个任务。

安排一个经常性的事件。

安排一个钩子,它将由WordPress在指定的时间间隔内触发。如果预定的时间已过,当有人访问你的WordPress网站时,该动作将被触发。

循环事件的有效值是”每小时”、”每天”和”每天两次”。这些可以使用wp_get_schedules()中的{@see ‘cron_schedules’}过滤器来扩展。

使用wp_next_scheduled()来防止重复的事件。

使用wp_schedule_single_event()来安排一个非周期性事件。

function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
	// Make sure timestamp is a positive integer.
	if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
		if ( $wp_error ) {
			return new WP_Error(
				'invalid_timestamp',
				__( 'Event timestamp must be a valid Unix timestamp.' )
			);
		}

		return false;
	}

	$schedules = wp_get_schedules();

	if ( ! isset( $schedules[ $recurrence ] ) ) {
		if ( $wp_error ) {
			return new WP_Error(
				'invalid_schedule',
				__( 'Event schedule does not exist.' )
			);
		}

		return false;
	}

	$event = (object) array(
		'hook'      => $hook,
		'timestamp' => $timestamp,
		'schedule'  => $recurrence,
		'args'      => $args,
		'interval'  => $schedules[ $recurrence ]['interval'],
	);

	/** This filter is documented in wp-includes/cron.php */
	$pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error );

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

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

		return $pre;
	}

	/** This filter is documented in wp-includes/cron.php */
	$event = apply_filters( 'schedule_event', $event );

	// A plugin disallowed this event.
	if ( ! $event ) {
		if ( $wp_error ) {
			return new WP_Error(
				'schedule_event_false',
				__( 'A plugin disallowed this event.' )
			);
		}

		return false;
	}

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

	$crons = _get_cron_array();

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

	return _set_cron_array( $crons, $wp_error );
}

常见问题

FAQs
查看更多 >