Cronjobs at night – but not the rest of the day

To achieve the behavior you’re describing in WordPress, where a cron job runs hourly but only between 2 AM and 5 AM, you have a couple of options. While WordPress doesn’t offer this level of specificity in its default scheduling, you can customize it to suit your needs.

Custom Cron Scheduling: This involves creating a custom cron schedule in WordPress that checks the current time and then decides whether to run the task. Here’s an outline of how you might implement it:

Create a function that hooks into the ‘cron_schedules’ filter to add a custom schedule that runs hourly.
In your scheduled event’s callback function, check if the current time is between 2 AM and 5 AM. If it is, execute the task; if not, do nothing.

Here’s a basic example in PHP:

Add this to your themes functions.php file at the bottom. (Use a child theme if you can)

add_filter('cron_schedules', 'wpb_custom_cron_schedule');
function wpb_custom_cron_schedule($schedules) {
    $schedules['hourly_2am_5am'] = array(
        'interval' => 3600, // Every hour
        'display' => __('Hourly between 2 AM and 5 AM')
    );
    return $schedules;
}

add_action('wpb_hourly_event', 'wpb_do_this_hourly');
function wpb_do_this_hourly() {
    $current_hour = date('G'); // 'G' format will return hours in 24-hour format without leading zeros

    if ($current_hour >= 2 && $current_hour <= 5) {
        // Your code here
    }
}

if (!wp_next_scheduled('wpb_hourly_event')) {
    wp_schedule_event(time(), 'hourly_2am_5am', 'wpb_hourly_event');
}

Multiple Daily Schedules: As you mentioned, another way is to schedule the event to run daily at specific times (2 AM, 3 AM, 4 AM, and 5 AM). This is a bit redundant but works if you prefer a more straightforward approach.

Each of these methods has its advantages. The custom cron scheduling offers more flexibility and is more efficient, as it involves fewer scheduled tasks. The multiple daily schedules method is simpler and might be easier to manage if you’re not comfortable with more complex code.

Remember to replace the placeholder comments and function names with your actual task logic and appropriate naming conventions.

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino