Good day, dear blog readers!
Let’s continue dissecting our table plugin “piece by piece.” In this part, we’ll take a look at our table class: “Plance_Table_Lessons2.”
When we open the class, the first method we’ll see is `prepare_items()`. It’s responsible for preparing data to be displayed. Calling this method is mandatory. Here’s the method code:
public function prepare_items()
{
//Sets
$per_page = $this -> get_items_per_page('plance_per_page', 10);
/* Get data to build the table */
$data = $this -> table_data();
/* Set pagination data */
$this -> set_pagination_args( array(
'total_items' => count($data),
'per_page' => $per_page
));
/* Slice array for pagination */
$data = array_slice(
$data,
(($this -> get_pagenum() - 1) * $per_page),
$per_page
);
$this -> _column_headers = $this -> get_column_info();
/* Set table items */
$this -> items = $data;
}
The line below gets the number of items per page for pagination. We configured this in the previous article — see the `showScreenOptions` method.
$per_page = $this -> get_items_per_page('plance_per_page', 10);
Pagination settings are user-specific and are stored in the `wp_usermeta` table. The record can be found by the key `plance_per_page`.
In this lesson, we slightly simplified our code and replaced:
$this -> _column_headers = array( $this -> get_columns(), $this -> get_hidden_columns(), $this -> get_sortable_columns() );
with:
$this -> _column_headers = $this -> get_column_info();
which looks more elegant 🙂
Let’s update the `get_columns()` method, which defines the column headers. We’ll also add a checkbox as the first array element to support bulk actions:
public function get_columns()
{
return array(
'cb' => '<input type="checkbox" />',
....
);
}
Now we need to add an additional column to render checkboxes for each item. This is done in a separate method:
function column_cb($item)
{
return '<input type="checkbox" name="id[]" value="'.$item['ex_id'].'" />';
}
There’s no need to explain this method — it’s pretty straightforward.
We also need to add links to edit and delete each row. For that, we’ll create another method named after the column:
function column_ex_title($item)
{
return $item['ex_title'].' '.$this -> row_actions(array(
'edit' => '<a href="?page='.$_REQUEST['page'].'&action=edit&book='.$item['ex_id'].'">edit</a>',
'delete' => '<a href="?page='.$_REQUEST['page'].'&action=delete&book='.$item['ex_id'].'">delete</a>',
));
}
In this case, using the `row_actions()` method, we attach two links to the item title. The key is the link identifier, and the value is the link HTML or any other content. You can add as many links as you like. How you format the URLs is entirely up to you.
The last method we’ll review in this article is the one that defines the dropdown for bulk actions:
function get_bulk_actions()
{
return array(
'delete' => __('delete', 'plance'),
'lock' => __('lock', 'plance'),
'unlock' => __('unlock', 'plance'),
);
}
As you can see — everything is simple. Just practice for a day or two and it will all become clear.
In the next parts of this series, we’ll look at connecting the table with real data from the database.
UPD. 2021-06-04. I’ve added a GitHub example of implementing a list table in the WP admin panel. You can download it from this link.
