wp_cron()为所有预定事件运行预定回调或批量生产定时任务

目录

描述

译文

为所有预定事件运行预定回调或批量生产定时任务。

原文

Run scheduled callbacks or spawn cron for all scheduled events.

wp_cron() 描述

用法

<?php wp_cron() ?>

wp_cron() 用法

参数

None.

wp_cron() 参数

返回值

(null) 

When cron doesn't need to run..

wp_cron() 返回值

示例

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

if ( ! wp_next_scheduled( 'my_task_hook' ) ) {
  wp_schedule_event( time(), 'hourly', 'my_task_hook' );
}

add_action( 'my_task_hook', 'my_task_function' );

function my_task_function() {
  wp_mail( 'your@email.com', 'Automatic email', 'Automatic scheduled email from WordPress.');
}

wp_cron() 示例

注意

  • Cron is named after a unix program which runs unattended scheduled tasks.

wp_cron() 注意

历史

  • 添加于 版本: 2.1.0

wp_cron() 历史

源文件

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Run scheduled callbacks or spawn cron for all scheduled events.
 *
 * @since 2.1.0
 */
function wp_cron() {
	// Prevent infinite loops caused by lack of wp-cron.php
	if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
		return;

	if ( false === $crons = _get_cron_array() )
		return;

	$gmt_time = microtime( true );
	$keys = array_keys( $crons );
	if ( isset($keys[0]) && $keys[0] > $gmt_time )
		return;

	$schedules = wp_get_schedules();
	foreach ( $crons as $timestamp => $cronhooks ) {
		if ( $timestamp > $gmt_time ) break;
		foreach ( (array) $cronhooks as $hook => $args ) {
			if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
				continue;
			spawn_cron( $gmt_time );
			break 2;
		}
	}
}

wp_cron() 源文件

相关