Skip to content

Instantly share code, notes, and snippets.

@afragen
Last active November 21, 2024 12:03
Show Gist options
  • Save afragen/47e11b4df8513709889877d1bf0d82c7 to your computer and use it in GitHub Desktop.
Save afragen/47e11b4df8513709889877d1bf0d82c7 to your computer and use it in GitHub Desktop.
Display WordPress DB table names in a dashboard widget.
<?php
/**
* Display DB Tables Widget.
*
* @package Fragen\Display_DB_Tables
*
* Plugin Name: Display DB Tables Widget
* Plugin URI: https://gist.github.com/afragen/47e11b4df8513709889877d1bf0d82c7
* Description: Display WordPress DB table names in dashboard widget.
* Version: 0.1.0
* Author: Andy Fragen
* License: MIT
* Requires at least: 6.6
* Requires PHP: 7.4
* Gist Plugin URI: https://gist.github.com/afragen/47e11b4df8513709889877d1bf0d82c7
*/
namespace Fragen;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Let's get started.
add_action(
'wp_dashboard_setup',
function () {
if ( current_user_can( 'manage_options' ) ) {
wp_add_dashboard_widget( 'display_wp_db_tables', 'Display DB Tables', __NAMESPACE__ . '\get_db_tables' );
}
}
);
/**
* Get listing of DB tables.
*
* @return void
*/
function get_db_tables(): void {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$db_tables = $wpdb->get_results( 'SHOW TABLES', ARRAY_N );
$names = array_map(
function ( $table ) {
return $table[0];
},
$db_tables,
);
$names = implode( '<br>', $names );
echo( wp_kses_post( $names ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment