Using WordPress ‘delete_get_calendar_cache()’ PHP function

The delete_get_calendar_cache() WordPress PHP function purges the cached results of the get_calendar function.

Usage

You can use delete_get_calendar_cache() function when you want to ensure that the calendar data is up-to-date, for example after a new event has been added or an existing event has been modified or deleted.

delete_get_calendar_cache();

This function doesn’t require any parameters and doesn’t return any output. After running this function, the next call to get_calendar() will fetch fresh data.

Parameters

  • This function does not have any parameters.

More Information

See WordPress Developer Resources: delete_get_calendar_cache

This function was implemented in WordPress 5.5.0 and is not yet deprecated. It’s part of the WordPress core, so you can use it in any WordPress theme or plugin.

Examples

Update Calendar Cache After Adding New Event

// Assume $event is a new event
add_event($event);
// Immediately clear the calendar cache
delete_get_calendar_cache();

This example shows how you might use delete_get_calendar_cache() immediately after adding a new event to ensure the calendar is updated.

Clear Calendar Cache After Deleting an Event

// Assume $event_id is the ID of an event to be deleted
delete_event($event_id);
// Clear the calendar cache after the event is deleted
delete_get_calendar_cache();

In this example, the cache is cleared after an event is deleted to ensure the calendar no longer shows the deleted event.

Clear Calendar Cache After Modifying an Event

// Assume $event is an event that has been modified
update_event($event);
// Clear the calendar cache after the event is modified
delete_get_calendar_cache();

This example demonstrates clearing the cache after an event has been modified, to ensure the calendar reflects the updated event details.

Clear Calendar Cache When a Plugin is Activated

// Clear the calendar cache when a plugin is activated
function my_plugin_activate() {
    delete_get_calendar_cache();
}
register_activation_hook( __FILE__, 'my_plugin_activate' );

This example shows how you might clear the calendar cache when a plugin is activated.

Clear Calendar Cache When a Plugin is Deactivated

// Clear the calendar cache when a plugin is deactivated
function my_plugin_deactivate() {
    delete_get_calendar_cache();
}
register_deactivation_hook( __FILE__, 'my_plugin_deactivate' );

In this last example, the calendar cache is cleared when a plugin is deactivated, which might be useful if your plugin affects the events displayed on the calendar.