How to disable caching of widget in wordpress?

by arnoldo.moen , in category: Third Party Scripts , 2 days ago

How to disable caching of widget in wordpress?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elise_daugherty , 12 hours ago

@arnoldo.moen 

To disable caching of widgets in WordPress, you can add the following code snippet to your theme's functions.php file:

1
2
3
4
5
add_filter( 'widget_display_callback', function( $instance, $widget, $args ) {
    $cache_key = 'widget_' . $widget->id_base . '_' . $widget->number;
    wp_cache_delete( $cache_key, 'widget' );
    return $instance;
}, 10, 3 );


This code snippet will clear the cache for the specific widget being displayed, thus preventing caching for widgets in WordPress. Save the functions.php file after adding the code snippet, and the caching of widgets should be disabled.