Display Custom Table Data in WordPress Admin
For plugin or theme developers, displaying custom table data in the WordPress admin area is a common requirement. Using a custom table, the developer can present information in a convenient and organized manner within the WordPress dashboard.
Why Display Custom Table Data in WordPress Admin
There are several reasons to display custom table data in WordPress Admin. In the following line, we will explain one by one.
Custom tables permit developers to present data in a way that is logical for their particular use case. The custom table can be formatted and arranged in a better way to convey the information effectively. The custom table can be sorted and filtered to improve usability.
The custom tables within the WordPress admin are helpful to ensure integration with core features. It is possible to add core features like search, pagination, and bulk actions by creating a custom table within the WordPress admin. This data can be accessed by users using the WordPress functionality.
Another use of custom tables is to display various settings and configurations in a plugin. By organizing plugin options in a better way, users can easily understand them.
A custom table is used to display important information about the site. Essential metrics, logs, or other crucial information that administrators may need to regularly monitor can be organized in Custom Tables.
For data retrieval from large datasets that are not suitable for WordPress post types or custom post types, custom tables can be utilized efficiently. To enhance performance, developers can use pagination and other techniques to custom tables.
How to Display Custom Table Data in WordPress Admin
Here we will explain the steps to Display Custom Table Data in WordPress Admin. First, you have to create a custom table in the WordPress database. if want to learn how to Create Custom Database Table in WordPress see the article.
After creating a table we can display a custom table in the admin area. You can create a plugin or display by inserting code in the function file. We will create a plugin to display custom the table in WordPress admin.
Assume we have created a custom table and now we have created a plugin named Display Custom Table Data
Create a WordPress Plugin
Create a file display-custom-table-data.php and place it in the WordPress plugin folder(wp-content/plugins). Add code at the top of the file.
/*
Plugin Name: Display Custom Table Data
Description: Displaying custom table data in the WordPress admin.
Version: 1.0
Author: Your Name
*/
To display Custom Table Data we also need to create a menu page in the admin area. If you don’t know how to create a menu page check this tutorial. We will display custom table data in WordPress Admin integrating this menu page.
Create a Menu Page in The Admin Area
Now add the following code to the plugin file for the display menu page.
function plugin_menu_page() {
add_menu_page(
'Custom Table Display',
'Custom Table Display',
'manage_options',
'custom-table',
'fun_to_display_custom_table',
'',
'1'
);
}
add_action( 'admin_menu', 'plugin_menu_page' );
function fun_to_display_custom_table() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
// here add code to display custom table
echo '<h1>Custom table will be displayed here</h1>';
}
This code will create menu link and page(Display Custom Table) as shown below in the picture.
To Display Custom Table Data in WordPress Admin, we will add our custom table code to the function fun_to_display_custom_table
Displaying Custom Table Data in WordPress Admin
First, we will create a WP database query to fetch custom table data. We have added this database query to the function display_table(). We will call this function inside the fun_to_display_custom_table function.
The $wpdb global object provided by WordPress is an instantiation of the wpdb class. By default, the WordPress database is accessed by $wpdb.
Here we will fetch data from the table socity_members and we will add prefix $wpdb->prefix to the table, and store table name in variable $table.
function display_table(){
global $wpdb;
$table = $wpdb->prefix . 'socity_members';
$query = "SELECT * FROM $table";
$query = $wpdb->prepare( $query );
$table_data = $wpdb->get_results( $query, OBJECT );
foreach($table_data as $table_datas){
$name = $table_datas->name;
$father_name = $table_datas->father_name;
$vill = $table_datas->vill;
$dist = $table_datas->dist;
$address = $table_datas->address;
}
}
Displaying Fetched data in The Form of Table
function display_table(){
global $wpdb;
$table = $wpdb->prefix . 'socity_members';
$query = "SELECT * FROM $table";
$query = $wpdb->prepare( $query );
$table_data = $wpdb->get_results( $query, OBJECT );
echo "<table>";
echo "<tr>";
echo "<th>Name</th><th>Father Name</th><th>City/Vill</th><th>Dist</th><th>Address</th>";
echo "</tr>";
foreach($table_data as $table_datas){
$name = $table_datas->name;
$father_name = $table_datas->father_name;
$vill = $table_datas->vill;
$dist = $table_datas->dist;
$address = $table_datas->address;
echo "<tr>";
echo "<td>". $name ."</td><td>". $father_name ."</td><td>". $vill ."</td><td>"
. $dist ."</td><td>". $address ."</td>";
echo "</tr>";
}
echo "</table>";
}
Full Code of Custom Table
This is the full code of the plugin to display custom table data in WordPress admin.
<?php
/*
Plugin Name: Display Custom Table Data
Description: Displaying custom table data in the WordPress admin.
Version: 1.0
Author: Your Name
*/
function plugin_menu_page() {
add_menu_page(
'Custom Table Display',
'Custom Table Display',
'manage_options',
'custom-table',
'fun_to_display_custom_table',
'',
'1'
);
}
add_action( 'admin_menu', 'plugin_menu_page' );
function fun_to_display_custom_table() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
echo '<h1>Custom table will be displayed here</h1>';
display_table();
}
function display_table(){
global $wpdb;
$table = $wpdb->prefix . 'socity_members';
$query = "SELECT * FROM $table";
$query = $wpdb->prepare( $query );
$table_data = $wpdb->get_results( $query, OBJECT );
echo "<table>";
echo "<tr>";
echo "<th>Name</th><th>Father Name</th><th>City/Vill</th><th>Dist</th><th>Address</th>";
echo "</tr>";
foreach($table_data as $table_datas){
$name = $table_datas->name;
$father_name = $table_datas->father_name;
$vill = $table_datas->vill;
$dist = $table_datas->dist;
$address = $table_datas->address;
echo "<tr>";
echo "<td>". $name ."</td><td>". $father_name ."</td><td>". $vill ."</td><td>"
. $dist ."</td><td>". $address ."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Conclusion
The WordPress admin area can display custom table data for a variety of benefits, including improved data organization, efficient data retrieval, enhanced user experience, and seamless integration with core features. This approach helps to Present custom information and functionalities in the WordPress dashboard.
In this tutorial, we have explained all the steps about how to display custom table data in WordPress Admin. We hope this tutorial helps you to display custom table data.