Make WP_List_Table columns selectable from screen options
https://www.ads-software.com/support/topic/make-wp_list_table-columns-selectable-from-screen-options
I have been working on a plugin using WP_List_Table and have worked my through the adventures with “Screen Options”. Here are some suggestions:
1. In your __construct(), enable Ajax:
//Set parent defaults
parent::__construct( array(
'singular' => 'attachment', //singular name of the listed records
'plural' => 'attachments', //plural name of the listed records
'ajax' => true //false //does this table support ajax?
) );
2. Implement a “manage_{$page}_columns” filter, and use it on your get_columns() function:
function get_columns(){
$columns = mla_manage_columns();
return $columns;
}
3. Use the “manage{$page}columnshidden” option, maintained by WordPress core:
function get_hidden_columns(){
$columns = (array) get_user_option( 'managemedia_page_mla-menucolumnshidden' );
return $columns;
}
4. Implement the get_sortable_columns function, e.g.:
function get_sortable_columns() {
$sortable_columns = array(
'ID_parent' => array('ID_parent',false), //true means it's already sorted
'title_name' => array('title_name',true)
);
return $sortable_columns;
}
5. Implement the _column_headers property in your prepare_items() function:
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
6. Implement the “manage_{$page}_columns” filter:
/*
* This filter/function is required because the list_table object
* isn't created in time.
*/
function mla_manage_columns (){
$columns = array(
'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
'icon' => '',
'ID_parent' => 'ID/Parent',
'title_name' => 'Title/Name',
'featured' => 'Featured in',
'inserted' => 'Inserted in'
);
return $columns;
}
add_filter( "manage_media_page_mla-menu_columns", 'mla_manage_columns', 0 );
7. There is a geat tutorial on how to set up the “posts per page” option here:
https://chrismarslender.com/wp-tutorials/wordpress-screen-options-tutorial/
Let me know if you have questions. Good luck!