Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#15386 closed defect (bug) (worksforme)

WP_List_Table::get_columns does not work for plugins

Reported by: ptahdunbar Owned by: scribu
Milestone: Priority: high
Severity: major Version: 3.1
Component: General Keywords: needs-patch
Focuses: Cc:


Creating a new table based on the WP_List_Table class does not work as the get_columns method is not being called.

The problem is when WP_List_Table::get_column_info() calls get_column_headers() instead of $this->get_columns() where all the column data is stored.

Moving the filter manage_*_columns from get_column_headers() into WP_List_Table::get_column_info() along with passing $this->get_columns() fixes this issue.

Attachments (4)

ticket.15386.get_columns.diff (634 bytes) - added by ptahdunbar 4 years ago.
my-list-table-class.php (1.4 KB) - added by scribu 4 years ago.
Example list table class
my-list-table-plugin.php (984 bytes) - added by scribu 4 years ago.
Example plugin that uses list table class
screen.diff (739 bytes) - added by manutdotcom 3 years ago.
remove array() arg from apply_filters

Download all attachments as: .zip

Change History (18)

comment:1 @scribu4 years ago

  • Owner set to scribu
  • Status changed from new to accepted

comment:2 @scribu4 years ago

  • Milestone changed from Awaiting Review to 3.1

comment:3 @scribu4 years ago

  • Keywords close added

The constructor in WP_List_Table adds the get_columns() method as filter with priority 0.

This is for backwards compatibility with older plugins that use the filter directly to register columns for new screens.

comment:4 @scribu4 years ago

The likely problem is that get_column_headers() is called before My_Plugin_List_Table is instantiated.

comment:5 @scribu4 years ago

  • Keywords needs-patch added; has-patch close removed

my-list-table-plugin.php creates an admin screen and uses an example list table class semi-successfully.

The ajax doesn't work however.

comment:6 @scribu4 years ago

  • Severity changed from normal to major

comment:7 @scribu4 years ago

  • Priority changed from normal to high

@scribu4 years ago

Example list table class

@scribu4 years ago

Example plugin that uses list table class

comment:8 @scribu4 years ago

  • Milestone 3.1 deleted
  • Resolution set to worksforme
  • Status changed from accepted to closed

I've only needed to hook into 'get_list_table_*'.

my-list-table-plugin.php now succesfully registers a custom list table class.

@manutdotcom3 years ago

remove array() arg from apply_filters

comment:9 @manutdotcom3 years ago

  • Cc manutdotcom added

When the array() argument is removed from line 24 (see screen.diff attachment), the table does tabulate properly without the need of initializing the WP_List_Table class before the page is loaded using add_action( "load-$screen_id", array( $this, '_init_list_table' ) ) as recommended in my-list-table-plugin.php above. The WP_List_table class can be initialized inside the function that was declared for add_menu_page().

Then I would be able to dynamically render different tables according to the GET variable. Useful for tabbed pages.

I was wondering since get_columns() do not require an input argument, maybe we could remove array() from line 24?

comment:10 @scribu3 years ago

In the meantime, WP_List_Table has become internal i.e. is not recommended to be used directly by plugins at this time (WP 3.3).

With this in mind, feel free to open a new ticket.

comment:11 @scribu3 years ago

But before that, see #17615 and #18449

comment:12 @manutdotcom3 years ago

Thanks, in the meantime, I'll just manually define $this->_column_headers instead of it being automatically populated.

comment:13 follow-up: @markoheijnen3 years ago

  • Cc marko@… added

What does of means that it is private. This class is a brilliant asset a developer can use since you want the same look in your plugin as WordPress itself.

Is the code most likely to change? Some information here can be handy for developers who want to use it.

comment:14 in reply to: ↑ 13 @dd323 years ago

Replying to markoheijnen:

What does of means that it is private. This class is a brilliant asset a developer can use since you want the same look in your plugin as WordPress itself.

The private status in this case means exactly what Private access means in applications "This should only be used by the core application at this point in time". It'll be rehashed elsewhere, but put basically: Don't use this while it's Private and expect core to retain compatibility in future releases, It'll be changed and will most likely break whatever you're currently doing with it in a million piece.

It's going to be a useful functionality to Plugin developers, We're just not quite at the point where we can offer it up to developers.

Note: See TracTickets for help on using tickets.