Extendable for WP_list_tables?
Is there a way to extend the functionality of the plugin to a custom WP_list_table? The functionality of the plugin is awesome on the base admin lists. I have to create a few custom lists though and wondering if there is an easy way to extend the plugin for these lists?
I’m glad you like working with Admin Columns, that’s always good to hear!
What you request is certainly possibly. However, it’s not exactly trivial to achieve. The following paragraphs should get you started, but they might be quite technical.
Admin Columns is built around
WP_List_Table: for the most important types of
WP_List_Table, we have a corresponding “storage model”, which is a class that implements behaviour for that specific list table and the associated data. We have storage models for posts (which includes posts, pages and custom post types), users, comments, media and taxonomies. What you’ll end up doing in the end is creating your own storage model for your
All storage model classes extend our base storage model class,
classes/storage_model.php). This abstract base class does most of the hard work, all dependent on the properties defined in the extending classes.
One of these extending classes is
classes/storage_model/user.php). This class handles Admin Columns functionality for the users overview page (
wp_admin/users.php). You could use this as a starting point to create your own class extending
Next, every storage model has columns associated with it. These classes are located in
classes/column/[storage_model]/[column-name].php. The default column types are automatically loaded from the
WP_List_Table, but other custom columns would have to be defined separately. You can find out how to do so in this tutorial.
You can then link your storage model to Admin Columns via the
cac/storage_models-filter, which filters the list of storage models. It is an array with storage model keys (
CPAC_Storage_Model:$key) as the array keys and the corresponding storage model class instance as the array values.
I hope I’ve given you enough information to get going with creating your own storage model!
With kind regards,
Thanks for the point in the right direction Jesper! I dug through it a little bit last night and this will definitely help. I’m on a tight timeline and just ended up coding everything I need the hard way.
I look forward to having a little time to play with this and see if I can build myself a little extension I can replicate on future projects. Ultimately, it’s all about abstracting… I’ve learned so much about WP_List_Table in the last two weeks… I’m sure you know my pain:)
In the latest release we added support for taxonomies by extending our own CPAC_Storage_Model. You will find a complete example inside the folder
admin-columns-pro/pro/classes/taxonomy. You can take that folder and place it inside a plugin as a basic starting point.
I looked into the taxonomy example but it doesn’t do sorting and that is the part I’m most baffled by. I have it working for the default post stuff. I’m having trouble getting a column to search on the Post Title (Post IDs). It works perfectly in my the default list for the post type. Is there a particular spot in the code I can look at as an example of this for the get_sortable_columns function of WP_List_Table? I’ve tried searching around in the code but haven’t had much luck. I’m pulling out my last bit of hair…
Thanks for your help!
You can add sorting support to a custom (posttype) column by adding
$this->properties['is_sortable'] = true; to it’s constructor. Sorting will then use the
get_raw_value() for it’s sorting. This only works for the posttype columns
CPAC_Storage_Model_Post as of version 3.0.
In the documentation you will find a full example: https://www.admincolumns.com/documentation/how-to/register-column/
You must be logged in to reply to this topic.