WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 9 months ago

#42692 new defect (bug)

Unused static variable $column_headers

Reported by: jlambe Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: close dev-feedback
Focuses: administration Cc:
PR Number:

Description

Context

We're working on a custom WP_List_Table class. For a project, we have setup a custom admin page and attach it to a custom post type.

Basically we call the add_submenu_page and set its parent slug to edit.php?post_type=custom_post_slug.

Our custom list table class is called CurrencyListTable and just displays a list of currencies coming from a custom database table. For this table, we have defined 2 custom columns. We correctly extend the get_columns() method and return an array with those 2 columns.

Expected result

On our page display fallback, we instantiate our custom table class and call its prepare_item(), search_box() and display() methods in order to render our list table.

We expect to see the usual list table with the table header and footer, the rows containing 20 items as our $per_page definition, the search box and the table pagination.

Result

Everything is displayed on screen except the header, footer and rows of the table. The search box, pagination and total number of items is correctly displayed.

Solution

We have digged into the WordPress source code and stop our attention on the get_column_headers() function defined into the wp-admin/includes/screen.php file line 19.

When we remove the static definition for the $columns_header variable on line 23, our custom table renders correctly.

Perhaps a core developer could provide more information on why this variable is set as static.

We can see that the get_column_headers() function is called several times when rendering a list table and that we might want to re-use the set variable. But we don't understand why the first initialization is incorrect and set our column headers as an empty array.

When removing the static definition, everything works as expected. We have tested this with another custom set of people data where we have 114589 rows and haven't noticed any slow performance (we still show 20 rows per page).

Core list tables also work as expected for posts, pages, plugins, users, ...

Personnaly I think we can remove the static keyword as the WP_List_Table class get_column_info() method is storing the list of columns for a table instance into its "private" property $_column_headers. But I would like more insights from core developers before diggin into a patch.

Attachments (1)

wordpress_list_table_issue.jpg (186.9 KB) - added by jlambe 2 years ago.
Screenshot of the issue.

Download all attachments as: .zip

Change History (4)

@jlambe
2 years ago

Screenshot of the issue.

#1 @jlambe
2 years ago

  • Keywords close added

Without removing the static keyword, we were able to draw the table correctly by using an action hook load-{$page_hook} from the wp-admin/admin.php file.

When rendering our page with the later hook $page_hook, the issue persists. Because of the requirement of the admin header php file, we were not sure we could use the hook and trigger errors by rendering some content too early in the process.

I still would like some insights on this but the issue can be closed.

#2 @Presskopp
2 years ago

  • Keywords dev-feedback added

#3 @pento
9 months ago

  • Version trunk deleted
Note: See TracTickets for help on using tickets.