Using WP_List_Table to Create WordPress Admin Tables

Originally published at: http://www.sitepoint.com/using-wp_list_table-to-create-wordpress-admin-tables/

In the WordPress dashboard, the tables that displays the posts, pages and user data are all created internally by WordPress using the WP_List_Table PHP class.

Below are a couple of screenshots of the post and user admin pages:

As a plugin developer, the need to build a custom table that will contain a given data might arise. Rather than code your own table design, it’s best you use that of WordPress in order for your plugin settings page to conform to WordPress UI.

While you might be tempted to copy the HTML and CSS table design by viewing the source code of WordPress, you shouldn’t because WP_List_Table class there to help.

Continue reading this article on SitePoint

Hi,

Great tutorial/template for a custom db table, thanks

1/ I notice you are using php 5.4 i am still working with php 5.3, do you have sourcecode which doesn’t use 5.4 code ( i.e. Array dereferencing". PHP 5.4. )
-found a solutiion by replacing the with array()

2/ How do we add/enable a searchbox?

3/How to style with even and uneven row colors?

regards,

@barry If you use an IDE like PhpStorm, switch PHP language level to 5.3 then use its built-in tool-tip bulb to automatically convert 5.4 arrays to 5.3 and to spot all 5.4 features like anonymous functions.

I didn’t include the search box in order to keep the tutorial at a reasonable length for easy assimilation.
You might want to check out the link below for the search box and row styling

All the best.

Hi,
when I try to delete the selected options I get the ‘You do not have sufficient permissions to access this page.’ message. I also notice that in the url in the place where it should be the id (costumer[id]), I just have it blank. Is it because I’m using custom table? I don’t know what I’m doing wrong, so any help would be appreciated.

You are getting that error probably because the page the delete action is redirecting to doesn’t exist or not properly registered with add_menu_page() or add_submenu_page() functions.

Do you need the sql dump for the customer database i used?

I have tried this on my own code, where I use custom post type and custom database table. I have changed the code a little, but still get the same error. This is the code for the dele:

function column_student($item){
        $actions=array(
        'edit'=>sprintf('<a href="?post_type=%s&page=%s&action=%s&student=%s">Edit</a>',$_REQUEST['post_type'], $_REQUEST['page'], 'edit', $item->students_id),
        'delete'=>sprintf('<a href="?post_type=%s&page=%s&action=%s&student=%s">Delete</a>', $_REQUEST['post_type'], $_REQUEST['page'], 'delete', $item->students_id),
        
        );
        //Return the title contents
        return sprintf('%1$s <span style="color:silver"></span>%2$s',
            /*$1%s*/ $item->students_name,
            /*$2%s*/ $this->row_actions($actions)
        );
    }

function process_bulk_action(){
        if( 'delete'===$this->current_action() ) {
                $this->bsp_delete_student(($_REQUEST['student']));
        }
    }
    function bsp_delete_student($id){
        global $wpdb;
        $tablename=$wpdb->prefix."students"; //geting our table name with prefix
        $result=$wpdb->query("DELETE FROM '.$tablename.' WHERE students_id='.$id'");
    }
    
    function column_cb($item){
        return sprintf('<input type="checkbox" name="%1$s" value="%2$s" />', 
        $this->_args['singular'],  //$1%s   we use the tables singular label
        $item->students_id        //$2%s    the value of the chechbox is the students id
        );
    }

The rollover edit and delete are not showing anymore and I don’t know why. But when I click on the apply for bulk action it just redirects me to You do not have sufficient permissions to access this page. In the URL I now see the correct id’s, but nothing happens.
It’s also interesting that when I use your code and just change the database and it’s fields to mine, the mouse over delete “button” is not showing. Is this because I’m working on localhost? And when I try to delete the selected items it gives my an Warning: Cannot modify header information - headers already sent by (…wp-includes\pluggable.php on line 1196, but when I click again on the menu the record is deleted. Any ideas?

I have managed to not get an error for not having permission. I just changed the method from get to post in the form. But now every time I try to delete a record using your code for deletion, I just get the Go get a life script kiddies, which means that the nonce isn’t going trough. And if I comment out the nonce check, I get a warning headers already sent and no deletion of a record happens. Any ideas?

Double check to ensure your nonce created with wp_create_nonce() is sp_delete_customer

If you are using a different nonce value, be sure to replace sp_delete_customer with your nonce value in

if ( ! wp_verify_nonce( $nonce, 'sp_delete_customer' ) )

Yes, I checked it. Still getting the else statement. It seems like the function custom_name where the nonce is created isn’t called or something. Because it does not create the nonce with that name. And I don’t get the mouse over action of delete (row actions).
Does it got something to do that it is in a submenu inside a custom post type menu?

Are you sure all columns in your table are all defined in get_columns() method?

Ensure you add the exit keyword after every wp_redirect like so wp_redirect('url here'); exit; to prevent any header already sent error.

You might want to create a GitHub gist with your code for me to peruse.

I have all the columns I want to be shown defined in the get_columns() method. Because i have a date when the data is added and an ID which I don’t show in the table. I have the exit at the end of wp_redirect. This is the github gist https://gist.github.com/anonymous/2fbf94727533c768109f with the code for wp_list_table. I have created a custom post and custom taxonomies and submenus to add to the table and to show the data from the table. So if you also need that, I can add that too. Thanks for helping me!

I removed the wp_redirect() and now I don’t get any headers errors and the delete works. Now I only have the problem with the nonce. It always fails.

Great tutorial!! I’ve setup an admin page where the bulk actions selected are saved as meta data for each user. Is there a way to change the bulk_actions select list to a multiple selection? I overrode the bulk_actions method to change to

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.