Make WordPress Core

Ticket #15386: my-list-table-class.php

File my-list-table-class.php, 1.4 KB (added by scribu, 16 years ago)

Example list table class

Line 
1<?php
2
3class My_List_Table extends WP_List_Table {
4
5 function My_List_Table() {
6 parent::WP_List_Table( array(
7 'items',
8 ) );
9 }
10
11 function prepare_items() {
12 $this->items = $this->get_items();
13
14 $this->set_pagination_args( array(
15 'total_items' => 10,
16 'per_page' => 5
17 ) );
18 }
19
20 function check_permissions() {
21 if ( !current_user_can('manage_options') )
22 wp_die(__('Cheatin&#8217; uh?'));
23 }
24
25 function get_columns() {
26 return array(
27 'cb' => '<input type="checkbox" />',
28 'name' => __( 'Name', 'cck' ),
29 'color' => __( 'Color', 'cck' ),
30 );
31 }
32
33 function get_sortable_columns() {
34 return array(
35 'name' => 'name',
36 );
37 }
38
39 function column_cb( $item ) {
40 return '<input type="checkbox" name="delete_items[]" value="' . $item->id . '">';
41 }
42
43 function column_name( $item ) {
44 return $item->name;
45 }
46
47 function column_color( $item ) {
48 return $item->color;
49 }
50
51 function column_default( $item, $column_name ) {
52 return apply_filters( 'manage_items_custom_column', '', $column_name, $item->id );
53 }
54
55 function get_items() {
56 $items = array();
57
58 $page = $this->get_pagenum();
59
60 $range = range(($page-1) * 5, $page * 5);
61 if ( isset( $_REQUEST['order'] ) && 'desc' == $_REQUEST['order'] )
62 $range = array_reverse($range);
63
64 foreach ( $range as $i ) {
65 $items[] = (object) array(
66 'id' => $i,
67 'name' => "Item " . zeroise($i, 2),
68 'color' => 'red',
69 );
70 }
71
72 return $items;
73 }
74}
75