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

Customize Woocommerce Settings Tab

Let’s talk about customizing Woocommerce Admin setting Tabs.To hide a specific Woocommerce setting tab or want to customize the Woocommerce setting tabs, not the entire sub-menu, but just a tab.For just Removing tabs we can use

Add Custom Product Data Tab

Learn how to enhance your WooCommerce product editor by adding a custom product data tab. Follow a step-by-step guide using the woocommerce_product_data_tabs filter and the woocommerce_product_data_panels action. Create a custom tab with your desired label, and include your own fields or content within the panel. Customize your product data to collect and display additional information, providing a tailored experience for your WooCommerce products.

Adding Sidebar In WordPress Theme

Adding a sidebar to a WordPress theme involves modifying the theme files and registering a new sidebar area using WordPress functions. Below are the general steps to add a sidebar in WordPress themes:

WooCommerce: Get Product Info (ID, SKU, $) From $product Object

In WooCommerce, the `$product` object represents a single product, and it contains various information and methods that you can access to get details about the product. Here is a list of common product information that you can retrieve from the `$product` object: