Delete Expired Transients From the Admin Bar
Adds a toolbar button for administrators that removes expired transients from the database in one click, keeping the options table lean.
Opens WPIDE on
Code
add_action( 'admin_bar_menu', function ( $bar ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$bar->add_node( array(
'id' => 'wpide-clear-transients',
'parent' => 'top-secondary',
'title' => 'Clear expired transients',
'href' => wp_nonce_url( admin_url( 'admin-post.php?action=wpide_clear_transients' ), 'wpide_clear_transients' ),
) );
}, 999 );
add_action( 'admin_post_wpide_clear_transients', function () {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( 'Sorry, you are not allowed to do that.' );
}
check_admin_referer( 'wpide_clear_transients' );
delete_expired_transients( true );
wp_safe_redirect( add_query_arg( 'wpide_transients_cleared', '1', wp_get_referer() ? wp_get_referer() : admin_url() ) );
exit;
} );
add_action( 'admin_notices', function () {
if ( isset( $_GET['wpide_transients_cleared'] ) && current_user_can( 'manage_options' ) ) { // phpcs:ignore WordPress.Security.NonceVerification
echo '<div class="notice notice-success is-dismissible"><p>Expired transients deleted.</p></div>';
}
} );
Notes
Only expired transients are deleted, so cached data that is still valid is kept.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.