WP_LIST_TABLE - complete code for searching, listing, pagination:
1. First register a menu to maintain a view:
<?php
require_once 'class-people-list-table.php';
add_action( 'admin_menu', 'ashique_register_custom_menu_page' );
function ashique_register_custom_menu_page()
{
$parent_slug = 'ashique-slug';
$capability = 'manage_options';
add_menu_page(
'Title',
'Menu Title',
$capability,
$parent_slug,
'ashique_dashbard_callback',
'dashicons-dashboard',
6
);
add_submenu_page(
$parent_slug,
'Dashboard',
'Dashboard',
$capability,
$parent_slug,
'ashique_dashbard_callback',
);
add_submenu_page(
$parent_slug,
'People List',
'People List',
$capability,
'ashique-people-list',
'ashique_people_list_callback',
);
}
2. Now maintain list callback function:
function ashique_people_list_callback()
{
// Creating an instance of People_List_Table
$people_list_table = new People_List_Table();
$people_list_table->prepare_items();
?>
<div class="wrap">
<h2>People List Table</h2>
<form method="get">
<input type="hidden" name="page" value="<?php echo $_REQUEST['page']; ?>" />
<?php $people_list_table->search_box('Search', 'search_id'); ?>
</form>
<form method="post">
<input type="hidden" name="page" value="my_list_test" />
<?php $people_list_table->display(); ?>
</form>
</div>
<?php
}
3. List table class file code:
<?php
if (!class_exists('WP_List_Table'))
{
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class People_List_Table extends WP_List_Table
{
// Define table columns
function get_columns()
{
$columns = array(
'id' => 'ID',
'name' => 'Name',
'email' => 'Email',
);
return $columns;
}
// Get table data
private function get_table_data($args = [])
{
global $wpdb;
$people_table = $wpdb->prefix . 'people';
$search = $args['s'];
$offset = $args['offset'];
$number = $args['number'];
if ($search != '')
{
$sql = "SELECT id, name, email
FROM $people_table
WHERE
name LIKE '%$search%'
OR
email LIKE '%$search%'
ORDER BY email
LIMIT $offset, $number";
}
else
{
$sql = "SELECT id, name, email
FROM $people_table
ORDER BY email
LIMIT $offset, $number";
}
return $wpdb->get_results($sql, ARRAY_A);
}
private function get_table_data_total_rows($args = [])
{
$search = $args['s'];
global $wpdb;
$donar_table = $wpdb->prefix . 'people';
if ($search != '')
{
$sql = "SELECT id, name, email
FROM $people_table
WHERE
name LIKE '%$search%'
OR
email LIKE '%$search%'
ORDER BY name";
}
else {
$sql = "SELECT id, name, email
FROM $people_table
ORDER BY name";
}
return count($wpdb->get_results($sql, ARRAY_A));
}
function column_default($item, $column_name)
{
switch ($column_name)
{
case 'name':
case 'email':
default:
return $item[$column_name];
}
}
// Bind table with columns, data and all
function prepare_items()
{
//data
$per_page = 30;
$current_page = $this->get_pagenum();
// TODO: Implement your search logic here
$search = (isset($_REQUEST['s'])) ? sanitize_text_field($_REQUEST['s']) : '';
$args = array(
'offset' => ($current_page - 1) * $per_page,
'number' => $per_page,
's' => $search,
);
// TODO: Implement your data retrieval logic based on $args
$data = $this->get_table_data($args); // Replace with your data array
$total_items = $this->get_table_data_total_rows($args); // Replace with your data array
$this->set_pagination_args(array(
'total_items' => $total_items,
'per_page' => $per_page,
));
$columns = $this->get_columns();
$hidden = array();
$sortable = array();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $data;
}
}
.:Happy coding:.
No comments:
Post a Comment