Tag

reports Archives - Custom Web Application Developers in Bromley, London

Wordpress create admin reports using WP_List_Table

WordPress create admin reports using WP_List_Table

By | PHP, Plugins, Programming, Wordpress | No Comments

In this post I’m going to discuss how to use WP_List_Table to create reports/tables/lists that are similar in style to the standard WordPress reports for Posts, Pages etc.

To achieve this I’ll use the WP_List_Table core WordPress class, this contains the functionality to create said tables/reports.

This example will look at the links table and display the data from it. This code used here is adapted from an example plugin on WordPress.org by Matt Van Andel

Important information to mention about creating admin reports using WP_List_Table

Its important to mention that this class is marked private by WordPress and they could change it in the future, without warning  (I’d be surprised if they did, given alot of peoples code relies on this class, but you never know). To be extra safe you can make a copy of the class in your own code and include this.

Explanation of the Code

The complete source code for the how to create admin reports using WP_List_Table plugin, is below. I’ve stripped out alot of the original comments. Please download the original plugin from WordPress.org to see the original version with comments (my version is adapted to run a SQL query and output the results as a report, the original doesn’t use SQL instead a pre-populated array is used for simplicity).

I’ll discuss in brief what the plugin does, first include the links report (this is hidden by default now in wp), I’m going to use this functionality to load wp_links with data that I can show on my report (for testing purposes):

Next the WP_List_Table class is included, this isn’t available by default to plugins. Then a class is created that inherits from WP_List_Table (this class will override the necessary methods of its parent class to give us the functionality we require).

Following this we have the column_default and get_columns methods. Column default tells class what to print in the report for each column. get_columns tells the class what our columns are.

The next part is the prepare_items method, this is where the magic happens. We query the database and setup the required information for our pagination and so on. After this the final 2 functions deal with putting a menu item on the left of the admin dashboard and the actual report display and we’re all good.

I hope you’ve found this interesting. Creating admin reports using WP_List_Table, is very useful for creating reports that are in the style of wordpress admin.

The complete code for how to create admin reports using WP_List_Table