Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.