Skip to content

Instantly share code, notes, and snippets.

@chwnam
Created July 4, 2020 14:15
Show Gist options
  • Save chwnam/ce383048f1c251a933ef890a2b977207 to your computer and use it in GitHub Desktop.
Save chwnam/ce383048f1c251a933ef890a2b977207 to your computer and use it in GitHub Desktop.
Cron Study: WP 크론 API 동작을 위한 실험 플러그인.
<?php
/**
* Plugin Name: Cron Study
* Description: WP 크론 API 동작을 위한 실험 플러그인.
*/
register_activation_hook( __FILE__, function () {
if ( ! wp_next_scheduled( 'cron-study', [ 'time' => 'Next Thursday' ] ) ) {
$time = new DateTime( 'now', wp_timezone() );
$time->setTime(
$time->format( 'H' ),
intval( $time->format( 'i' ) ) + 1,
0
);
wp_schedule_event(
$time->getTimestamp(),
'every-minute',
'cron-study',
[ 'time' => 'Next Thursday' ]
);
}
} );
add_filter( 'cron_schedules', function ( $schedules ) {
$schedules['every-minute'] = [
'interval' => MINUTE_IN_SECONDS,
'display' => 'Every Minute',
];
return $schedules;
} );
register_deactivation_hook( __FILE__, function () {
if ( wp_next_scheduled( 'cron-study', [ 'time' => 'Next Thursday' ] ) ) {
wp_clear_scheduled_hook( 'cron-study', [ 'time' => 'Next Thursday' ] );
}
} );
add_action( 'cron-study', function () {
$message = sprintf(
'크론이 %s에 실행되었습니다. 인자: %s',
wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ),
preg_replace( "/[\r\n]+/", ' ', print_r( func_get_args(), 1 ) )
);
error_log( $message );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment