Forums

Make WP_List_Table columns selectable from screen options (4 posts)

  1. dpegasusm
    Member
    Posted 5 months ago #

    I am using the WP_List_Table class in a plugin to display rows from a custom table in the database and would like to give the user the option of turning on and off columns in the table from the screen options menu on the top of the page. I have tried looking at the code for other pages and seeing if i can figure it out as well as digging through the class-wp-list-table.php code itself but all i can find is 2 functions from the screen.php file called get_column_headers() and get_hidden_columns() that have little to no documentation as to what they are doing or how to work with them.

    The bottom line is am i on the right track here or is this far easier than i am making it? Is there anybody who knows how to accomplish this or could point me in the direction of a tutorial or a solution?

  2. garydarling
    Member
    Posted 3 months ago #

    This may be a dead issue to the OP, but in case someone else finds this thread, there is a plugin you can examine that provides a useful example of all the methods inside the class WP_list_table.

    http://wordpress.org/extend/plugins/custom-list-table-example/

  3. dpegasusm
    Member
    Posted 3 months ago #

    Thanks That plugin is extremely helpful and i used it to get 90% of the application i am trying to build done. However, the screen options stuff is not in there and i have yet to find any reference or documentation of it.

  4. dglingren
    Member
    Posted 1 month ago #

    Make WP_List_Table columns selectable from screen options
    http://wordpress.org/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:

    http://chrismarslender.com/wp-tutorials/wordpress-screen-options-tutorial/

    Let me know if you have questions. Good luck!

Reply

You must log in to post.

About this Topic