Cron Job WordPress (WP-Cron)

In WordPress, you can customize the schedule intervals for WP-Cron using the `cron_schedules`, `wp_schedule_event`, and various time constants. By defining your custom cron schedules and using `wp_schedule_event`, you can schedule tasks to run at intervals such as hourly, twice daily, daily, or even per minute. Here’s how you can achieve that:

To define custom cron schedules for hourly, twice daily, daily, per minute, every 10 seconds, and for every 10 days in WordPress, you can use the `cron_schedules` filter. Here’s how you can achieve this:

// Define custom cron schedules
add_filter('cron_schedules', 'custom_cron_schedules');
function custom_cron_schedules($schedules) {
    // Hourly schedule
    $schedules['hourly'] = array(
        'interval' => HOUR_IN_SECONDS,
        'display'  => __('Hourly', 'text-domain')
    );

    // Twice daily schedule
    $schedules['twicedaily'] = array(
        'interval' => DAY_IN_SECONDS / 2,
        'display'  => __('Twice Daily', 'text-domain')
    );

    // Daily schedule
    $schedules['daily'] = array(
        'interval' => DAY_IN_SECONDS,
        'display'  => __('Daily', 'text-domain')
    );

    // Per minute schedule
    $schedules['every_minute'] = array(
        'interval' => 60,
        'display'  => __('Every Minute', 'text-domain')
    );

    // Every 10 seconds schedule
    $schedules['every_10_seconds'] = array(
        'interval' => 10,
        'display'  => __('Every 10 Seconds', 'text-domain')
    );

    // Every 10 days schedule
    $schedules['every_10_days'] = array(
        'interval' => DAY_IN_SECONDS * 10,
        'display'  => __('Every 10 Days', 'text-domain')
    );

    return $schedules;
}

 

In the code above, we have added custom cron schedules for hourly, twice daily, daily, per minute, every 10 seconds, and every 10 days. We use the `HOUR_IN_SECONDS`, `DAY_IN_SECONDS`, and multiplication by 10 to define the intervals for each schedule.

Now, you can use these custom schedules with `wp_schedule_event` to schedule tasks accordingly:

// Schedule a task to run every hour
wp_schedule_event(time(), 'hourly', 'my_hourly_event');

// Schedule a task to run twice daily
wp_schedule_event(time(), 'twicedaily', 'my_twice_daily_event');

// Schedule a task to run every day
wp_schedule_event(time(), 'daily', 'my_daily_event');

// Schedule a task to run every minute (using the custom schedule we defined earlier)
wp_schedule_event(time(), 'every_minute', 'my_minute_event');

// Schedule a task to run every 10 seconds (using the custom schedule we defined earlier)
wp_schedule_event(time(), 'every_10_seconds', 'my_10_seconds_event');

// Schedule a task to run every 10 days (using the custom schedule we defined earlier)
wp_schedule_event(time(), 'every_10_days', 'my_10_days_event');

 

Replace `’my_hourly_event’`, `’my_twice_daily_event’`, `’my_daily_event’`, `’my_minute_event’`, `’my_10_seconds_event’`, and `’my_10_days_event’` with your actual event names and callback functions.

// Define the callback function for 'my_hourly_event'
function my_hourly_event_callback() {
    // Your task logic goes here
}
add_action('my_hourly_event', 'my_hourly_event_callback');

// Define the callback function for 'my_twice_daily_event'
function my_twice_daily_event_callback() {
    // Your task logic goes here
}
add_action('my_twice_daily_event', 'my_twice_daily_event_callback');

// Define the callback function for 'my_daily_event'
function my_daily_event_callback() {
    // Your task logic goes here
}
add_action('my_daily_event', 'my_daily_event_callback');

// Define the callback function for 'my_minute_event'
function my_minute_event_callback() {
    // Your task logic goes here
}
add_action('my_minute_event', 'my_minute_event_callback');

// Define the callback function for 'my_10_seconds_event'
function my_10_seconds_event_callback() {
    // Your task logic goes here
}
add_action('my_10_seconds_event', 'my_10_seconds_event_callback');

// Define the callback function for 'my_10_days_event'
function my_10_days_event_callback() {
    // Your task logic goes here
}
add_action('my_10_days_event', 'my_10_days_event_callback');

 

Remember that WP-Cron relies on site visits, so tasks may not run precisely on time, especially for per minute, every 10 seconds, and every 10 days intervals. For more precise and reliable scheduling, consider using a real server cron job as described in previous responses.

Categories

Related Blogs

How to know “Who created this coupon?”

What to do when your client ask “Who created this coupon?” . in WooCommerce.  But we don’t have any default things in WooCommerce  which automatically show author of coupon .

How to hide particular category product on shop

To hide a particular category on the shop page using the `woocommerce_product_query` hook, you can modify the query parameters to exclude the category you want to hide. This approach allows you to customize the product query directly without modifying the main query.