Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use cron_schedules filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
May 16, 2023
5 minutes read

Welcome to the world of WordPress hooks! Here, we’ll talk about the cron_schedules filter. As a fellow developer, you know how crucial it is to customize and extend WordPress functionality. Let’s dive into how you can use this filter to tweak the default cron schedules in WordPress with ease!

cron_schedules filter

Filters the non-default cron schedules.

To use the cron_schedules filter, you first need to register it using add_filter. You can insert this code into the functions.php file of your active theme or create a custom WordPress Plugin. At WePlugins, we always recommend creating a custom plugin to ensure your changes remain intact during theme updates.

In the examples below, we demonstrate how to define a function weplugins_modify_cron_schedules_defaults and register it using add_filter. The parameters include the hook name cron_schedules, the function name, priority of hook execution, and the number of arguments to pass to the function.

Sometimes, you might need to remove a registered hook, and for that, you can use remove_filter to remove the cron_schedules filter.

Example 1: Adding a Weekly Schedule

Below is an example of how you can add a custom ‘weekly’ schedule to the default cron schedules.

    function weplugins_add_weekly( $schedules ) {
        $schedules['weekly'] = array(
            'interval' => 604800,
            'display' => __('Once Weekly')
        );
        return $schedules;
    }
    add_filter( 'cron_schedules', 'weplugins_add_weekly' );
    

Example 2: Modify Default Schedules

Here’s how you can modify the existing cron schedules based on your website’s requirements.

    function weplugins_modify_cron_schedules_defaults($new_schedules) { 
        // Update $new_schedules as needed
        return $new_schedules; 
    }
    add_filter( "cron_schedules", "weplugins_modify_cron_schedules_defaults", 10, 1 );
    

Example 3: Removing a Hook Callback

If you need to remove a previously added hook callback, here’s how you can do it.

    remove_filter( "cron_schedules", "weplugins_modify_cron_schedules_defaults", 10, 1 );
    

Ensure you use the same callback function name, priority, and number of arguments when removing the hook.

Access Premium WordPress Plugins

Contact Us

If you need any customization or face challenges with this hook, feel free to reach out to us at WePlugins Contact Page. We’re here to help!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.