Transients Viewer Under Tools
Adds a Tools > Transients screen listing the cached transients stored in the database with their size and expiry time, and lets administrators delete a single one to force a refresh.
Opens WPIDE on
Code
add_action( 'admin_menu', function () {
add_management_page( 'Transients', 'Transients', 'manage_options', 'wpide-transients', function () {
global $wpdb;
if ( isset( $_POST['wpide_delete_transient'] ) && current_user_can( 'manage_options' ) && check_admin_referer( 'wpide_transients' ) ) {
$name = sanitize_text_field( wp_unslash( $_POST['wpide_delete_transient'] ) );
delete_transient( $name );
echo '<div class="notice notice-success"><p>' . esc_html( sprintf( 'Deleted transient %s.', $name ) ) . '</p></div>';
}
$rows = $wpdb->get_results(
"SELECT o.option_name, LENGTH(o.option_value) AS bytes, t.option_value AS expires
FROM {$wpdb->options} o
LEFT JOIN {$wpdb->options} t ON t.option_name = CONCAT('_transient_timeout_', SUBSTRING(o.option_name, 12))
WHERE o.option_name LIKE '\\_transient\\_%' AND o.option_name NOT LIKE '\\_transient\\_timeout\\_%'
ORDER BY bytes DESC LIMIT 200"
);
echo '<div class="wrap"><h1>Transients</h1>';
if ( ! $rows ) {
echo '<p>No transients are stored in the database.</p></div>';
return;
}
echo '<form method="post"><table class="widefat striped"><thead><tr><th>Name</th><th>Size</th><th>Expires</th><th></th></tr></thead><tbody>';
wp_nonce_field( 'wpide_transients' );
foreach ( $rows as $row ) {
$name = substr( $row->option_name, strlen( '_transient_' ) );
$expires = $row->expires ? ( $row->expires < time() ? 'expired' : 'in ' . human_time_diff( (int) $row->expires ) ) : 'never';
printf(
'<tr><td><code>%1$s</code></td><td>%2$s</td><td>%3$s</td><td><button class="button button-small" name="wpide_delete_transient" value="%1$s">Delete</button></td></tr>',
esc_attr( $name ),
esc_html( size_format( (int) $row->bytes ) ),
esc_html( $expires )
);
}
echo '</tbody></table></form></div>';
} );
} );
Notes
Sites with Redis or Memcached keep transients in the object cache, so the list will be short or empty there.
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.