Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Created May 23, 2021 10:00
Show Gist options
  • Save maheshwaghmare/03f663c09d4434303b91fe9a65f48183 to your computer and use it in GitHub Desktop.
Save maheshwaghmare/03f663c09d4434303b91fe9a65f48183 to your computer and use it in GitHub Desktop.
Display table with WP_List_Table - Read more at
<?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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment