Sometimes we need to display our own data in the table format same as the default WordPress tables like post, pages, custom post types etc.
We are going to see:
#Problem Problem
WordPress provides the class WP_List_Table which allow us to create our own custom tables.
Below is the screenshot of the page which display the custom table.
#Code Snippet Code Snippet
Use below code snippet and modify it according to your requirement.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Load the parent class if it doesn't exist. | |
if ( ! class_exists( 'WP_List_Table' ) ) { | |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; | |
} | |
/** | |
* Create Table | |
*/ | |
if ( ! class_exists( 'Prefix_My_Table' ) ) : | |
class Prefix_My_Table extends WP_List_Table { | |
/** | |
* Get a list of columns. | |
* | |
* @return array | |
*/ | |
public function get_columns() { | |
return array( | |
'name' => wp_strip_all_tags( __( 'Name' ) ), | |
'surname' => wp_strip_all_tags( __( 'Surname' ) ), | |
); | |
} | |
/** | |
* Prepares the list of items for displaying. | |
*/ | |
public function prepare_items() { | |
$columns = $this->get_columns(); | |
$hidden = array(); | |
$sortable = array(); | |
$primary = 'name'; | |
$this->_column_headers = array( $columns, $hidden, $sortable, $primary ); | |
} | |
/** | |
* Generates content for a single row of the table. | |
* | |
* @param object $item The current item. | |
* @param string $column_name The current column name. | |
*/ | |
protected function column_default( $item, $column_name ) { | |
switch ( $column_name ) { | |
case 'name': | |
return esc_html( $item['name'] ); | |
case 'surname': | |
return esc_html( $item['surname'] ); | |
return 'Unknown'; | |
} | |
} | |
/** | |
* Generates custom table navigation to prevent conflicting nonces. | |
* | |
* @param string $which The location of the bulk actions: 'top' or 'bottom'. | |
*/ | |
protected function display_tablenav( $which ) { | |
?> | |
<div class="tablenav <?php echo esc_attr( $which ); ?>"> | |
<div class="alignleft actions bulkactions"> | |
<?php $this->bulk_actions( $which ); ?> | |
</div> | |
<?php | |
$this->extra_tablenav( $which ); | |
$this->pagination( $which ); | |
?> | |
<br class="clear" /> | |
</div> | |
<?php | |
} | |
/** | |
* Generates content for a single row of the table. | |
* | |
* @param object $item The current item. | |
*/ | |
public function single_row( $item ) { | |
echo '<tr>'; | |
$this->single_row_columns( $item ); | |
echo '</tr>'; | |
} | |
} | |
endif; |
#Usage Usage
$table = new Prefix_My_Table(); $table->items = array( array( 'name' => 'Mahesh', 'surname' => 'Waghmare', ), array( 'name' => 'Deepak', 'surname' => 'Luckchaura', ), array( 'name' => 'Swapnil', 'surname' => 'Dhanrale', ), array( 'name' => 'Rohit', 'surname' => 'Toraskar', ) ); $table->prepare_items(); $table->display();