{
  "generator": "Code Snippets v3.8.2",
  "date_created": "2025-11-08 06:12",
  "snippets": [
    {
      "id": 173,
      "name": "SeeWPDB v1 - See WordPress Database Inside WP-Admin [SnipSnip.pro]",
      "desc": "<p>A simple admin tool to view all database tables, inspect their columns, and preview a few rows of data. No editing or destructive operations — just safe, read-only access for administrators. - https://snipsnip.pro/s/951</p>",
      "code": "\n/**\n * Title: See WordPress Database Inside WP-Admin [SnipSnip.pro]\n * Description: A simple admin tool to view all database tables, inspect their columns, and preview a few rows of data. No editing or destructive operations — just safe, read-only access for administrators.\n * Version: 1.0.0\n * Author: Brandon Pfeiffer\n * Last Updated: 2025-11-07\n * Blog URL: https://snipsnip.pro/s/951\n * Requirements: WordPress 6.0+, Administrator role\n * License: GPL v2 or later\n *\n * Changelog:\n * 1.0.0 (2025-11-07) - Initial release with table, column, and data preview display.\n */\n\nif (!class_exists('SeeWPDB')):\n\nclass SeeWPDB {\n    const VERSION = '1.0.0';\n\n    public function __construct() {\n        add_action('admin_menu', [$this, 'register_admin_page']);\n    }\n\n    /**\n     * Register the admin menu page\n     */\n    public function register_admin_page() {\n        add_menu_page(\n            __('See WP DB', 'seewpdb'),\n            __('See WP DB', 'seewpdb'),\n            'manage_options',\n            'seewpdb',\n            [$this, 'render_admin_page'],\n            'dashicons-database-view',\n            99\n        );\n    }\n\n    /**\n     * Render the admin page\n     */\n    public function render_admin_page() {\n        global $wpdb;\n\n        if (!current_user_can('manage_options')) {\n            wp_die(__('You do not have sufficient permissions to access this page.'));\n        }\n\n        // Handle table selection\n        $selected_table = isset($_GET['table']) ? sanitize_text_field($_GET['table']) : '';\n\n        echo '<div class=\"wrap\"><h1>SeeWPDB - WordPress Database Viewer</h1>';\n        echo '<p style=\"color:#555;\">Version ' . esc_html(self::VERSION) . ' — read-only table viewer.</p>';\n\n        // List all tables\n        echo '<h2>Database Tables</h2>';\n        $tables = $wpdb->get_col('SHOW TABLES');\n        if ($tables) {\n            echo '<ul style=\"column-count:3;list-style:disc;margin-left:20px;\">';\n            foreach ($tables as $table) {\n                $link = admin_url('admin.php?page=seewpdb&table=' . urlencode($table));\n                echo '<li><a href=\"' . esc_url($link) . '\">' . esc_html($table) . '</a></li>';\n            }\n            echo '</ul>';\n        } else {\n            echo '<p>No tables found.</p>';\n        }\n\n        // Show table info if one is selected\n        if ($selected_table) {\n            echo '<hr><h2>Table: ' . esc_html($selected_table) . '</h2>';\n\n            // Show metadata if available (engine, collation, etc.)\n            $table_status = $wpdb->get_row($wpdb->prepare(\"SHOW TABLE STATUS LIKE %s\", $selected_table));\n            if ($table_status) {\n                echo '<h3>Table Metadata</h3>';\n                echo '<table class=\"widefat striped\"><tbody>';\n                $metadata = [\n                    'Engine' => $table_status->Engine,\n                    'Rows' => $table_status->Rows,\n                    'Created' => $table_status->Create_time,\n                    'Updated' => $table_status->Update_time,\n                    'Collation' => $table_status->Collation,\n                    'Comment' => $table_status->Comment,\n                ];\n                foreach ($metadata as $key => $value) {\n                    echo '<tr><th style=\"width:150px;\">' . esc_html($key) . '</th><td>' . esc_html($value ?? '') . '</td></tr>';\n                }\n                echo '</tbody></table>';\n            }\n\n            // Show columns\n            $columns = $wpdb->get_results(\"SHOW COLUMNS FROM `$selected_table`\");\n            if ($columns) {\n                echo '<h3>Columns</h3>';\n                echo '<table class=\"widefat striped\"><thead><tr>';\n                echo '<th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th>';\n                echo '</tr></thead><tbody>';\n                foreach ($columns as $col) {\n                    echo '<tr>';\n                    echo '<td>' . esc_html($col->Field) . '</td>';\n                    echo '<td>' . esc_html($col->Type) . '</td>';\n                    echo '<td>' . esc_html($col->Null) . '</td>';\n                    echo '<td>' . esc_html($col->Key) . '</td>';\n                    echo '<td>' . esc_html($col->Default) . '</td>';\n                    echo '<td>' . esc_html($col->Extra) . '</td>';\n                    echo '</tr>';\n                }\n                echo '</tbody></table>';\n            }\n\n            // Show limited data rows\n            echo '<h3>Sample Data (first 10 rows)</h3>';\n            $rows = $wpdb->get_results(\"SELECT * FROM `$selected_table` LIMIT 10\", ARRAY_A);\n            if ($rows) {\n                echo '<div style=\"overflow:auto;max-height:400px;\"><table class=\"widefat striped\">';\n                echo '<thead><tr>';\n                foreach (array_keys($rows[0]) as $col_name) {\n                    echo '<th>' . esc_html($col_name) . '</th>';\n                }\n                echo '</tr></thead><tbody>';\n                foreach ($rows as $row) {\n                    echo '<tr>';\n                    foreach ($row as $value) {\n                        echo '<td>' . esc_html((string) $value) . '</td>';\n                    }\n                    echo '</tr>';\n                }\n                echo '</tbody></table></div>';\n            } else {\n                echo '<p><em>No data found or empty table.</em></p>';\n            }\n        }\n\n        echo '</div>';\n    }\n}\n\nendif;\n\nif (class_exists('SeeWPDB')):\n    new SeeWPDB();\nendif;\n",
      "tags": [
        "PHP",
        "WordPress admin",
        "database inspection",
        "read-only viewer",
        "developer tools",
        "database structure",
        "data preview",
        "manage_options",
        "db",
        "database"
      ],
      "scope": "admin",
      "active": true,
      "priority": 99,
      "modified": "2025-11-08 06:12:09",
      "revision": "1",
      "cloud_id": "21547_1"
    }
  ]
}