Halloween Sale 15% off Ends in See the plans
PHP

Searchable Options Viewer Under Tools

Adds a Tools > Options Viewer screen where administrators can search the options table by name and see each value decoded and pretty-printed, including serialized arrays that Settings > All hides.

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

Code

add_action( 'admin_menu', function () {
    add_management_page( 'Options Viewer', 'Options Viewer', 'manage_options', 'wpide-options-viewer', function () {
        global $wpdb;

        $search = isset( $_GET['wpide_option_search'] ) ? sanitize_text_field( wp_unslash( $_GET['wpide_option_search'] ) ) : '';

        echo '<div class="wrap"><h1>Options Viewer</h1><form method="get"><input type="hidden" name="page" value="wpide-options-viewer">';
        printf( '<label class="screen-reader-text" for="wpide-option-search">Option name</label><input type="search" id="wpide-option-search" name="wpide_option_search" value="%s" class="regular-text" placeholder="Option name contains..."> ', esc_attr( $search ) );
        submit_button( 'Search', 'secondary', '', false );
        echo '</form>';

        if ( strlen( $search ) < 3 ) {
            echo '<p>Type at least three characters of an option name.</p></div>';
            return;
        }

        $rows = $wpdb->get_results( $wpdb->prepare(
            "SELECT option_name, option_value, autoload FROM {$wpdb->options} WHERE option_name LIKE %s ORDER BY option_name LIMIT 100",
            '%' . $wpdb->esc_like( $search ) . '%'
        ) );

        $mask = function ( $value ) use ( &$mask ) {
            if ( is_array( $value ) || is_object( $value ) ) {
                $out = array();
                foreach ( (array) $value as $k => $v ) {
                    $out[ $k ] = preg_match( '/key|token|secret|pass|salt|auth/i', (string) $k ) ? '(hidden)' : $mask( $v );
                }
                return $out;
            }
            return $value;
        };

        echo '<table class="widefat striped" style="table-layout:fixed"><thead><tr><th style="width:28%">Name</th><th style="width:80px">Autoload</th><th>Value</th></tr></thead><tbody>';
        foreach ( $rows as $row ) {
            $value = maybe_unserialize( $row->option_value );
            $shown = preg_match( '/key|token|secret|pass|salt|auth/i', $row->option_name ) ? '(hidden)' : print_r( $mask( $value ), true );
            printf(
                '<tr><td><code>%s</code></td><td>%s</td><td><pre style="white-space:pre-wrap;word-break:break-all;margin:0;max-height:300px;overflow:auto">%s</pre></td></tr>',
                esc_html( $row->option_name ),
                esc_html( $row->autoload ),
                esc_html( mb_strimwidth( (string) $shown, 0, 5000, '...' ) )
            );
        }
        echo '</tbody></table></div>';
    } );
} );

Notes

Read-only; values that look like keys, tokens or passwords are masked.

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.