Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Creating WP List Tables by Hand

Posted on July 26, 2012 by Pippin in Intermediate, Tutorials, WordPress Admin / Dashboard, Writing Plugins 10 Comments
Home» Tutorials » Intermediate » Creating WP List Tables by Hand
Screenshot from 2012-07-26 10:37:18
Tweet
Love It - 1

The WP List Table is what we call the table of data that is displayed when you click on any post type in WordPress. For example, click on Posts and this will display a table listing all of the posts (or perhaps the latest 20) that you have created in WordPress. In this tutorial, we are going to look at how to create tables like this for our own custom data in plugins.

I use list tables anytime I need to show data in a plugin’s admin pages. Take a look at the screenshots below to see some real examples I have built.

Screen Shot 2012-07-26 at 10.53.38 AM
Screen Shot 2012-07-26 at 10.53.51 AM
Screen Shot 2012-07-26 at 10.54.14 AM
Screen Shot 2012-07-26 at 10.54.26 AM
Screen Shot 2012-07-26 at 10.54.52 AM
Screen Shot 2012-07-26 at 10.55.07 AM

There are several great reasons to display your data in a list table like these:

  1. Stays true to the WordPress core UI
  2. Requires zero custom CSS
  3. Takes less time to create than custom interfaces

Personally, I would say there are very, very few instances (if any) where you should not use a WP List Table for displaying data in your plugin, assuming the data is suited for a table/list layout. With that in mind, let’s take a look at the basic HTML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<table class="wp-list-table widefat fixed posts">
	<thead>
		<tr>
			<th><?php _e('Column Name', 'pippinw'); ?></th>
			<th><?php _e('Column Name', 'pippinw'); ?></th>
			<th><?php _e('Column Name', 'pippinw'); ?></th>
			<th><?php _e('Column Name', 'pippinw'); ?></th>
		</tr>
	</thead>
	<tfoot>
		<tr>
			<th><?php _e('Column Name', 'pippinw'); ?></th>
			<th><?php _e('Column Name', 'pippinw'); ?></th>
			<th><?php _e('Column Name', 'pippinw'); ?></th>
			<th><?php _e('Column Name', 'pippinw'); ?></th>
		</tr>
	</tfoot>
	<tbody>
		<tr>
			<td><?php _e('Column Data', 'pippinw'); ?></td>
			<td><?php _e('Column Data', 'pippinw'); ?></td>
			<td><?php _e('Column Data', 'pippinw'); ?></td>
			<td><?php _e('Column Data', 'pippinw'); ?></td>
		</tr>
		<tr>
			<td><?php _e('Column Data', 'pippinw'); ?></td>
			<td><?php _e('Column Data', 'pippinw'); ?></td>
			<td><?php _e('Column Data', 'pippinw'); ?></td>
			<td><?php _e('Column Data', 'pippinw'); ?></td>
		</tr>
	</tbody>
</table>

It is nothing more than a table with a few specific class names, a table header and footer, and then a table body with the data rows.

When using a list table in your plugin, it will probably be structured something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<table class="wp-list-table widefat fixed posts">
	<thead>
		<tr>
			<th><?php _e('First Column', 'pippinw'); ?></th>
			<th><?php _e('Second Column', 'pippinw'); ?></th>
			<th><?php _e('Third Column', 'pippinw'); ?></th>
		</tr>
	</thead>
	<tfoot>
		<tr>
			<th><?php _e('First Column', 'pippinw'); ?></th>
			<th><?php _e('Second Column', 'pippinw'); ?></th>
			<th><?php _e('Third Column', 'pippinw'); ?></th>
		</tr>
	</tfoot>
	<tbody>
	<?php
	$data_array = array('....'); // this array holds all of your data
	if( !empty( $data_array ) ) :
 
		foreach( $data_array as $row ) : ?>
			<tr>
				<td><?php echo $row['column_one_key']; ?></td>
				<td><?php echo $row['column_two_key']; ?></td>
				<td><?php echo $row['column_three_key']; ?></td>
			</tr>
			<?php
		endforeach;
	else : ?>
		<tr>
			<td colspan="3"><?php _e('No data found', 'pippinw'); ?></td>
		</tr>
		<?php 
	endif; 
	?>	
	</tbody>
</table>

All of the table rows are usually contained within an array, and then that array is looped through in order to show the information associated with each individual row or item in the table.

That’s it and this is all you need to create your basic table with all of the core WordPress styles.

Here are some tips for going further and creating more interactive tables:

1. To create “status” options (All, Published, Pending, etc), create an unordered list with the class of “subsubsub”, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<ul class="subsubsub">
	<li class="all">
		<a href="#" class="current">
			<?php _e('All'); ?> 
			<span class="count">(1)</span>
		</a> |
	</li>
	<li class="publish">
		<a href="#">
			<?php _e('Active'); ?> 
			<span class="count">(5)</span>
		</a>
	</li>
</ul>

You will have to add in your own query arguments and such, but that is the basic HTMl. This should be placed above your list table.

2. To paginate your data table, use the awesome paginate_links() function.

3. To make a column sortable, add classes like this:

1
2
3
4
5
6
<th class="manage-column column-title sortable">
	<a href="add_query_args_here">Sortable (but not sorted)</a>
</th>
<th class="manage-column column-title sortable sorted">
	<a href="add_query_args_here">Sortable (and currently sorted)</a>
</th>

4. To automate the table creation process, look into using the WP_List_Table class. I’m going to cover this class in a tutorial some time in the near future, so stay tuned.

Have questions about using list tables in your plugins, perhaps on creating more interactive tables? Just ask!

Tweet Follow @pippinsplugins
wp_list_table

10 comments on “Creating WP List Tables by Hand”

  1. M Chasen says:
    July 26, 2012 at 12:09 pm

    Thanks for this amazing tutorial Pippin! As far as I see it’s just a matter of using the core CSS classes, which brilliant because it can save so much development time.

    Reply
  2. Leland says:
    August 3, 2012 at 1:48 pm

    Pippin,

    Have you created any ajax searching tools for the WP post tables? We are looking to create this but couldn’t find any real-world examples around the WP community. Basically looking to search on custom field meta and categories etc in a header filter box above the post table for a custom post type. Thanks for tutorial.

    Reply
    • Pippin says:
      August 5, 2012 at 10:45 pm

      No I haven’t, sorry.

  3. nathan says:
    October 29, 2012 at 7:47 am

    I would love a simple way of adding a function so that say if a status checkbox ( three columns and each being a selectable status with a radio button type checkbox ( only one selected at a time ) ( i.e., in progress, almost done, and done) when a checkbox is clicked then the user/email (the email will be another field in the same row ) is then emailed with new status of that row/data…

    Reply
    • Pippin says:
      October 30, 2012 at 8:46 am

      Not quite following. Can you elaborate?

    • nathan says:
      October 30, 2012 at 2:52 pm

      http://www.logo-business.com/elite/files/2012/10/Untitled-1.jpg

      here is a pic of what i’m wanting to do…. After that is done i would like to add a ping that will email or notify me of changes in subscription so that the members status is updated in other website/software…

    • nathan says:
      October 30, 2012 at 2:54 pm

      sorry for typos – on some heavy meds – : )

    • Pippin says:
      October 30, 2012 at 7:52 pm

      Ah no, editing directly in the columns like that is very difficult.

  4. nathan says:
    October 29, 2012 at 7:48 am

    awesome post by the way! – thank YOu!

    Reply
  5. nathan says:
    October 30, 2012 at 9:48 pm

    one word – piklist – : ) shhhhhh!!!! don’t tell anyone else! -lol I’m going to have this ready by morning – if it does what it says it will do! array away thru the night! Check it !!! http://wordpress.org/extend/plugins/piklist/ Been using types and views plugin along with cred to try and get some headway on project and feel http://wordpress.org/extend/plugins/piklist/ is what i’ll try tonight! then I’ll learn the code needed as i go!!! : )

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

Sorry, no related items found.

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Test Your Plugins with RTL (0)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • @HipHopMakers should be back shorlty
    May 25, 2013
  • @mrpritchett good idea. Not in the plugin currently but I like the odea
    May 25, 2013
  • @mrpritchett what kind of short codes?
    May 25, 2013

Topics

hook meta box Rémi Corson featured shortcodes campaign monitor add_options_page register_setting Sugar Event Calendar attachments add_shortcode wp_enqueue_script the_content image forms short codes Related posts login do_action authors mail chimp attachment plugin recent posts comments post types bbpress apply_filters short code taxonomies custom post type Ajax images gallery Stripe jquery taxonomy users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins