Schedule a recurring action with Action Scheduler

/**
 * Schedules a recurring action.
 *
 * @see https://wpcodebook.com/schedule-recurring-action-scheduler/
 * @see https://actionscheduler.org/api/
 */
add_action( 'init', function () {

	if ( ! function_exists( 'as_schedule_recurring_action' ) ) {
		return;
	}

	if ( as_has_scheduled_action( 'wpcodebook' ) ) {
		return;
	}

	as_schedule_recurring_action(
		time(),      // timestamp
		60,          // interval in seconds
		'wpcodebook' // hook name
	);

} );

/**
 * Action example.
 */
add_action( 'wpcodebook', function () {
	error_log(
		date( 'Y-m-d H:i:s' ) . PHP_EOL,
		3,
		'wpcodebook-log.txt'
	);
} );

Leave a Comment