wp_schedule_event()计划某个钩子(hook)的运行周期

目录

描述

译文

计划某个钩子(hook)的运行周期,用户指定时间周期后,WordPress动作(action)函数每一周期内执行一次该钩子。预定时间过去后,如果有人访问用户的WordPress网站,动作函数钩子会被触发。插件API中有一份钩子(hook)列表。

原文

Schedules a hook which will be executed by the WordPress actions core on a specific interval, specified by you. The action will trigger when someone visits your WordPress site, if the scheduled time has passed. See the Plugin API for a list of hooks.

wp_schedule_event() 描述

用法

 <?php wp_schedule_event(time(), 'hourly''my_schedule_hook'$args); ?> 

wp_schedule_event() 用法

参数

$timestamp

(integer) (必填) The first time that you want the event to occur. This must be in a UNIX timestamp format.

默认值: None

$recurrance

(string) (必填) How often the event should reoccur. Valid values:

  • hourly
  • twicedaily
  • daily

默认值: None

$hook

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

默认值: None

$args

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

默认值: None

wp_schedule_event() 参数

示例

Schedule an hourly event

To schedule an hourly event in a plugin, call wp_schedule_event on activation (otherwise you will end up with a lot of scheduled events!):

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

register_activation_hook(__FILE__, 'my_activation');
add_action('my_hourly_event', 'do_this_hourly');

function my_activation() {
	wp_schedule_event(time(), 'hourly', 'my_hourly_event');
}

function do_this_hourly() {
	// do something every hour
}

Don't forget to clean the scheduler on deactivation:

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

register_deactivation_hook(__FILE__, 'my_deactivation');

function my_deactivation() {
	wp_clear_scheduled_hook('my_hourly_event');
}

wp_schedule_event() 示例