WordPress.org

Ready to get started?Download WordPress

Forums

Make WP_List_Table columns selectable from screen options (7 posts)

  1. dpegasusm
    Member
    Posted 2 years 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. Gary Darling
    Member
    Posted 2 years 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 2 years 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. David Lingren
    Member
    Posted 2 years 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!

  5. jmutton
    Member
    Posted 1 year ago #

    I've looked over what you've suggested and I just can't get my 'screen options' tab to appear! :-(

    Has anyone got a working example?

    Cheers,
    Jason

  6. David Lingren
    Member
    Posted 1 year ago #

    I had another look at my plugin code and found another "trick". You have to add the filters when the source file containing your list table class is loaded. You can't put these calls inside your class and wait until the object is created to run them. Here are the last few lines of my source file:

    } // class MLA_List_Table
    
    /*
     * Filters are added here, when the source file is loaded, because the MLA_List_Table
     * object is created too late to be useful.
     */
    add_filter( "get_user_option_managemedia_page_mla-menucolumnshidden", 'MLA_List_Table::mla_manage_hidden_columns', 10, 3 );
    add_filter( "manage_media_page_mla-menu_columns", 'MLA_List_Table::mla_manage_columns', 10, 0 );
    ?>

    Also, not the exact name of the filters, including the page-specific hooks. The punctuation is uneven. In my case, the page-specific hook is "managemedia_page_mla-menu". The rest of the filter name must be exactly as documented.

    Note that the filter handlers must either be declared outside the class or be declared as "public static function ", because they have to be available before the object is created.

  7. Gyrate360
    Member
    Posted 1 year ago #

    I have an example in my blog ,see:http://www.liyingqing.com/archives/1716

Topic Closed

This topic has been closed to new replies.

About this Topic