wp_get_schedules()


WordPressの関数wp_get_schedules()は、サイトで利用可能なすべてのスケジュールを取得するために使用されます。この関数は、カスタムスケジュールやデフォルトのスケジュールを含むすべてのスケジュールの詳細を返します。

構文

wp_get_schedules();
  • array — スケジュール名をキー、各スケジュールの詳細情報(間隔や説明など)を値とする連想配列。

使用例

例 1: すべてのスケジュールを取得する

この例では、利用可能なすべてのスケジュールを取得して表示します。

<?php
$schedules = wp_get_schedules();
print_r( $schedules );
?>

例 2: スケジュール名をリスト表示する

この例では、すべてのスケジュール名をリストとして出力します。

<?php
$schedules = wp_get_schedules();
foreach ( $schedules as $schedule => $details ) {
    echo $schedule . '<br>';
}
?>

例 3: スケジュールの間隔と説明を表示する

各スケジュールの間隔(秒)と説明を出力します。

<?php
$schedules = wp_get_schedules();
foreach ( $schedules as $schedule => $details ) {
    echo $schedule . ': ' . $details['interval'] . '秒 - ' . $details['display'] . '<br>';
}
?>

例 4: 特定のスケジュールが存在するか確認する

この例では、特定のスケジュール(例: ‘hourly’)が存在するか確認します。

<?php
$schedules = wp_get_schedules();
if ( isset( $schedules['hourly'] ) ) {
    echo 'Hourlyスケジュールは存在します。';
} else {
    echo 'Hourlyスケジュールは存在しません。';
}
?>

例 5: カスタムスケジュールの追加後に確認する

カスタムスケジュールを追加した後、それが正しく登録されているか確認します。

<?php
add_filter( 'cron_schedules', function( $schedules ) {
    $schedules['fifteen_minutes'] = array(
        'interval' => 900,
        'display'  => 'Every 15 Minutes'
    );
    return $schedules;
});
$schedules = wp_get_schedules();
if ( isset( $schedules['fifteen_minutes'] ) ) {
    echo 'カスタムスケジュールは登録されています。';
}
?>

注意事項

  • wp_get_schedules()は、他のプラグインやテーマが追加したカスタムスケジュールも含めてすべてのスケジュールを返します。
  • スケジュールの変更やカスタムスケジュールの追加は、cron_schedulesフィルタを使用してください。

関連機能: