Native Admin Tables in WordPress are very commonly used but are quite difficult to make. Many users find the process confusing and complex. But if dealt with systematically, better results can be produced. There are certain steps which are needed to be followed.
WordPress table Outlook:
At first glance, you are able to see various elements at the admin page. The page consists of various gadgets and header and footer. They enable you to take actions with the table and add or subtract details according to your wishes. This whole outlook remains the same, no matter how many editing you do. The central portion consists of the main body of the content.
The ‘Right’ Table in WordPress:
Soon you will get used to the common outlook of the table. When the main elements are clear, then it is the time for you to create the right kind of table for your page, one that is suitable for your requirements. The data is mostly is SQL table form.
The List Table Class:
By using WP_List_Table class, you don’t have to trouble to write HTML again and again. This tool is actually very powerful and enables ease for back-end developers. It actually eases the work of the user and lets him avoid the trouble for writing HTML again and again. It allows the WordPress user to actually focus on the other important things like data management. You can use the list table to create class_list_table.
Now that you have the key principle that goes with your data, you can now customize and edit according to your needs and desires. You can also add elements before and after the table although the overall outlook of the WordPress list tables remains the same.
Header and Footer for the List Table
The WordPress admin page is very consistent. As mentioned above, the page is consistent and the overall outlooks in terms of positioning of the elements remain the same. Every admin page has a header and footer at the start and the end of the page respectively. They display certain information that is almost probably same as that displayed by the main body. They usually tell about the title of the columns whether they are simple or linked.
Adding elements before and after the table:
In order to add contents before or after the table, you need to add extra_tablenav. This method takes one parameter named ‘$which’, which is usually easy and less time consuming. It can be called twice. When it is called for the first time, it is valued as ‘top’, and when it is called for the second time, it is valued as ‘below’ or ‘bottom’.
The method is implemented as follows,
/**
* Add extra markup in the toolbars before or after the list
* @param string $which, helps you decide if you add the markup after (bottom) or before (top) the list
*/
functionextra_tablenav( $which ) {
if ( $which == “top” ){
//The code that goes before the table is here
echo”Hello, I’m before the table”;
}
if ( $which == “bottom” ){
//The code that goes after the table is there
echo”Hi, I’m after the table”;
}
}
The Table’s Rows:
In order to make the list table enable to display the data, the method that is followed is called prepare_items method.
/**
* Prepare the table with different parameters, pagination, columns and table elements
*/
functionprepare_items() {
global $wpdb, $_wp_column_headers;
$screen = get_current_screen();
/* — Preparing your query — */
$query = “SELECT * FROM $wpdb->links”;
/* — Ordering parameters — */
//Parameters that are going to be used to order the result
$orderby= !empty($_GET[“orderby”]) ? mysql_real_escape_string($_GET[“orderby”]) : ‘ASC';
$order = !empty($_GET[“order”]) ? mysql_real_escape_string($_GET[“order”]) : ”;
if(!empty($orderby) & !empty($order)){ $query.=’ ORDER BY ‘.$orderby.’ ‘.$order; }
/* — Pagination parameters — */
//Number of elements in your table?
$totalitems = $wpdb->query($query); //return the total number of affected rows
//How many to display per page?
$perpage = 5;
//Which page is this?
$paged = !empty($_GET[“paged”]) ? mysql_real_escape_string($_GET[“paged”]) : ”;
//Page Number
if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
//How many pages do we have in total?
$totalpages = ceil($totalitems/$perpage);
//adjust the query to take pagination into account
if(!empty($paged) && !empty($perpage)){
$offset=($paged-1)*$perpage;
$query.=’ LIMIT ‘.(int)$offset.’,’.(int)$perpage;
}
/* — Register the pagination — */
$this->set_pagination_args( array(
“total_items” => $totalitems,
“total_pages” => $totalpages,
“per_page” => $perpage,
) );
//The pagination links are automatically built according to those parameters
/* — Register the Columns — */
$columns = $this->get_columns();
$_wp_column_headers[$screen->id]=$columns;
/* — Fetch the items — */
$this->items = $wpdb->get_results($query);
}
It is often considered difficult to actually create a PHP class. But if certain steps and regulations are followed, then it becomes quite easy to manage and create Native List Tables for the WordPress. Once the process is done, it is a sigh of relief to know that much of the functions are automatically dealt with.