class User_List_Table extends WP_List_Table { public function prepare_items() { // Get the list of users $users = get_users(); // Set up columns $columns = array( 'cb' => '', 'username' => 'Username', 'email' => 'Email', 'role' => 'Role', 'actions' => 'Actions' ); // Set up sortable columns $sortable_columns = array( 'username' => array('username', false), 'email' => array('email', false), 'role' => array('role', false) ); // Populate items $this->items = $users; // Set up columns and sortable columns $this->_column_headers = array($columns, array(), $sortable_columns); } public function column_default($item, $column_name) { switch($column_name) { case 'username': return $item->user_login; case 'email': return $item->user_email; case 'role': return implode(', ', $item->roles); case 'actions': return 'Edit | Delete'; default: return ''; } } } $user_list_table = new User_List_Table(); $user_list_table->prepare_items(); $user_list_table->display();
class Post_List_Table extends WP_List_Table { public function prepare_items() { // Get the list of posts $posts = get_posts(); // Set up columns $columns = array( 'cb' => '', 'title' => 'Title', 'author' => 'Author', 'date' => 'Date', 'actions' => 'Actions' ); // Set up sortable columns $sortable_columns = array( 'title' => array('title', false), 'author' => array('author', false), 'date' => array('date', false) ); // Populate items $this->items = $posts; // Set up columns and sortable columns $this->_column_headers = array($columns, array(), $sortable_columns); } public function column_title($item) { $actions = array( 'edit' => sprintf('Edit', $item->ID), 'trash' => sprintf('Trash', $item->ID) ); return sprintf('%1$s %2$s', $item->post_title, $this->row_actions($actions)); } public function column_author($item) { $author = get_userdata($item->post_author); return sprintf('%1$sThis example shows how to create another custom WP_List_Table class to display a list of posts. It uses the `get_posts()` function to get the list of posts, and sets up columns and sortable columns using arrays. It also defines custom `column_title()` and `column_author()` functions to format the post title and author information. Package library: WordPress.
%2$s', $author->display_name, $author->user_email); } } $post_list_table = new Post_List_Table(); $post_list_table->prepare_items(); $post_list_table->display();