Halloween Sale 15% off Ends in See the plans
PHP

Autoloaded Options Inspector

Adds Tools > Autoloaded Options showing the total size of options WordPress loads on every page and the 40 largest ones, with a button to stop autoloading any option that does not need it.

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

Code

add_action( 'admin_menu', function () {
    add_management_page( 'Autoloaded Options', 'Autoloaded Options', 'manage_options', 'wpide-autoload', function () {
        global $wpdb;

        $protected = array( 'siteurl', 'home', 'blogname', 'active_plugins', 'cron', 'template', 'stylesheet', 'rewrite_rules', 'permalink_structure', 'wp_user_roles', $wpdb->prefix . 'user_roles' );
        $autoload  = function_exists( 'wp_autoload_values_to_autoload' ) ? wp_autoload_values_to_autoload() : array( 'yes' );
        $in        = "'" . implode( "','", array_map( 'esc_sql', $autoload ) ) . "'";

        if ( isset( $_POST['wpide_noautoload'] ) && check_admin_referer( 'wpide_noautoload' ) && current_user_can( 'manage_options' ) ) {
            $name = sanitize_text_field( wp_unslash( $_POST['wpide_noautoload'] ) );
            if ( ! in_array( $name, $protected, true ) && function_exists( 'wp_set_option_autoload' ) ) {
                wp_set_option_autoload( $name, false );
                printf( '<div class="notice notice-success"><p>%s</p></div>', esc_html( 'Autoload turned off for ' . $name . '.' ) );
            }
        }

        $total = (int) $wpdb->get_var( "SELECT SUM(LENGTH(option_value)) FROM {$wpdb->options} WHERE autoload IN ($in)" );
        $rows  = $wpdb->get_results( "SELECT option_name, LENGTH(option_value) AS size FROM {$wpdb->options} WHERE autoload IN ($in) ORDER BY size DESC LIMIT 40" );

        echo '<div class="wrap"><h1>Autoloaded Options</h1>';
        printf( '<p>%s</p>', esc_html( sprintf( 'Autoloaded data loaded on every request: %s. Below about 800 KB is healthy.', size_format( $total, 1 ) ) ) );
        echo '<table class="widefat striped"><thead><tr><th>Option</th><th>Size</th><th></th></tr></thead><tbody>';
        foreach ( $rows as $row ) {
            printf( '<tr><td><code>%s</code></td><td>%s</td><td>', esc_html( $row->option_name ), esc_html( size_format( (int) $row->size, 1 ) ) );
            if ( ! in_array( $row->option_name, $protected, true ) && 0 !== strpos( $row->option_name, '_transient' ) ) {
                echo '<form method="post" style="margin:0" onsubmit="return confirm(\'Stop autoloading this option?\');">';
                wp_nonce_field( 'wpide_noautoload' );
                echo '<button class="button button-small" name="wpide_noautoload" value="' . esc_attr( $row->option_name ) . '">Stop autoloading</button></form>';
            }
            echo '</td></tr>';
        }
        echo '</tbody></table></div>';
    } );
} );

Notes

Turning autoload off is safe for options a plugin only reads now and then; core options such as siteurl, active_plugins and cron are protected.

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.