[wp-trac] [WordPress Trac] #34508: list_table_primary_column doesn't work following some manage_$screen_columns

WordPress Trac noreply at wordpress.org
Fri Oct 30 15:04:12 UTC 2015


#34508: list_table_primary_column doesn't work following some
manage_$screen_columns
----------------------------+-----------------------------
 Reporter:  soulseekah      |      Owner:
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Administration  |    Version:
 Severity:  normal          |   Keywords:
  Focuses:  administration  |
----------------------------+-----------------------------
 The `list_table_primary_column` filter short-circuits when using the
 `manage_users_columns` filter to add/remove columns instead of extending
 the `WP_List_Table` class.

 For example, let's add a column to `WP_Users_List_Table` and try setting
 it as primary:

 {{{#!php
 add_filter( 'manage_users_columns', function( $columns ) {
     $columns['company'] = 'Company';
     return $columns;
 } );
 }}}

 So far so good. Now let's set the 'company' column as primary.

 {{{#!php
 add_filter( 'list_table_primary_column', function( $column, $screen ) {
     if ( $screen != 'users' ) return;
     return 'company';
 }, null, 2 );
 }}}

 Expect it to work and yet it doesn't. Why?

 Let's take a look at the `get_primary_column_name` function in
 https://github.com/WordPress/WordPress/blob/master/wp-admin/includes
 /class-wp-list-table.php#L939

 See the short circuit?

 {{{#!php
 if ( empty( $column ) || ! isset( $columns[ $column ] ) ) {
     $column = $default;
 }
 }}}

 Well turns out that the `$columns` are retrieved via `get_columns` which
 is unfiltered in some subclasses. Namely `WP_Users_List_Table`, which
 returns:

 {{{#!php
 public function get_columns() {
     $c = array(
         'cb'       => '<input type="checkbox" />',
         'username' => __( 'Username' ),
         'name'     => __( 'Name' ),
         'email'    => __( 'Email' ),
         'role'     => __( 'Role' ),
         'posts'    => __( 'Posts' )
     );
     if ( $this->is_site_users )
         unset( $c['posts'] );
     return $c;
 }
 }}}

 Without reusing the filter there we're simply screwed. Note that this
 doesn't happen for all builtin subclasses. For example
 `WP_MS_Users_List_Table` does it right:
 https://github.com/WordPress/WordPress/blob/master/wp-admin/includes
 /class-wp-ms-users-list-table.php#L160

 So let's add the missing filters to `get_columns`, shall we?

--
Ticket URL: <https://core.trac.wordpress.org/ticket/34508>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list