WP List Table

WP_List_Table Core Reference 19

1. Key Methods to Override

MethodPurpose
prepare_items()Fetch/paginate/sort data (required)
get_columns()Define table columns (required)
column_default()Render content for columns without custom methods
column_{name}()Custom output for specific columns (e.g., column_title())
get_sortable_columns()Define which columns are sortable
get_bulk_actions()Add bulk actions (e.g., “Delete”, “Export”)

2. Essential Properties

PropertyDescription
$itemsArray of data to display
$_argsConfiguration (plural/singular labels, AJAX support)
$_pagination_argsPagination settings (total items, per page, etc.)

3. Basic Implementation

class Custom_List_Table extends WP_List_Table {
    public function __construct() {
        parent::__construct([
            'singular' => 'item',  // Used for nonces
            'plural'   => 'items', // Used for CSS classes
            'ajax'     => false
        ]);
    }

    // Fetch data (example)
    public function prepare_items() {
        $data = $this->fetch_data(); // Your query logic
        $this->items = $data;
        $this->set_pagination_args([
            'total_items' => count($data),
            'per_page'    => 20
        ]);
    }

    // Define columns
    public function get_columns() {
        return [
            'cb'    => '<input type="checkbox">',
            'title' => 'Title',
            'date'  => 'Date'
        ];
    }

    // Render "Title" column
    public function column_title($item) {
        return sprintf('<a href="%s">%s</a>', $item['url'], $item['title']);
    }
}

Advanced Features

Bulk Actions:

public function get_bulk_actions() {
    return ['delete' => 'Delete'];
}
public function process_bulk_action() {
    if ('delete' === $this->current_action()) {
        // Handle bulk delete
    }
}

Search Box:

public function search_box($text, $input_id) {
    parent::search_box('Search Items', 'search_id');
}

Row Actions:

public function column_title($item) {
    $actions = [
        'edit'   => sprintf('<a href="?page=edit&id=%s">Edit</a>', $item['id']),
        'delete' => sprintf('<a href="?page=delete&id=%s">Delete</a>', $item['id'])
    ];
    return sprintf('%s %s', $item['title'], $this->row_actions($actions));
}

Common Pitfalls & Solutions 810

  1. Class Not Found Error:
    Ensure WP_List_Table is loaded with:
if (!class_exists('WP_List_Table')) {
    require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}