• dustyryan85

    (@dustyryan85)


    I downloaded a plugin template designed as a ‘live learning’ example for the WP_List_Table function.

    I have learned a lot from it and I want to use parts of it to improve a plugin i am currently using.

    The problem is that it has a ‘sample data array’ instead of a query.

    Example:

    <?php
    /*
    Plugin Name: Custom List Table Example
    */
    if(!class_exists('WP_List_Table')){
        require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
    }
    
    class TT_Example_List_Table extends WP_List_Table {
    
        var $example_data = array(
                array(
                    'ID'        => 1,
                    'title'     => '300',
                    'rating'    => 'R',
                    'director'  => 'Zach Snyder'
                ),
                array(
                    'ID'        => 2,
                    'title'     => 'Eyes Wide Shut',
                    'rating'    => 'R',
                    'director'  => 'Stanley Kubrick'
                ),
                array(
                    'ID'        => 3,
                    'title'     => 'Moulin Rouge!',
                    'rating'    => 'PG-13',
                    'director'  => 'Baz Luhrman'
                ),
                array(
                    'ID'        => 4,
                    'title'     => 'Snow White',
                    'rating'    => 'G',
                    'director'  => 'Walt Disney'
                ),
                array(
                    'ID'        => 5,
                    'title'     => 'Super 8',
                    'rating'    => 'PG-13',
                    'director'  => 'JJ Abrams'
                ),
                array(
                    'ID'        => 6,
                    'title'     => 'The Fountain',
                    'rating'    => 'PG-13',
                    'director'  => 'Darren Aronofsky'
                ),
                array(
                    'ID'        => 7,
                    'title'     => 'Watchmen',
                    'rating'    => 'R',
                    'director'  => 'Zach Snyder'
                ),
                array(
                    'ID'        => 8,
                    'title'     => '2001',
                    'rating'    => 'G',
                    'director'  => 'Stanley Kubrick'
                ),
            );
    
    		function __construct(){
            global $status, $page;
    
            //Set parent defaults
            parent::__construct( array(
                'singular'  => 'movie',     //singular name of the listed records
                'plural'    => 'movies',    //plural name of the listed records
                'ajax'      => false        //does this table support ajax?
            ) );
    
        }
    }

    The example data is like a movie database. The data I am after will come from the options table in wpdb.

    The code I have so far is:

    $example_data = get_option('limit_login_lockouts');
    foreach($example_data as $value1 => $value2) {
    	echo $value1 . " => " . $value2 . "<br />";
    }

    It echos an array just fine but I can’t figure out how to make the table show the values.
    70.193.96.113 => 1447706565

    Here is a link to the plugin I’m getting the code from
    so you can see the entire code.

    I have pulled up every reference to WP_List_Table and get_options and array I could find online but still haven’t figured it out.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter dustyryan85

    (@dustyryan85)

    Oh and here is how the data is stored in the database. Im sure it will help to see this.
    a:1:{s:13:"70.193.96.113";i:1447706565;}

    Moderator bcworkz

    (@bcworkz)

    The topic is too complex to go into any detail here, hopefully this will point you in the right direction. I’m not sure what the plugin offers, I would look at actual core examples to learn how to extend WP_List_Table.

    Glossing over many details, you need to override get_columns() to return a column definition array. If you then look at WP_List_Table::single_row_columns() you can see you need a method named 'column_' . $column_name for each column in any row that outputs all of your table HTML for any one $item that appears in that column inside the <td></td> cell elements of the table.

    Your column method can get data for $item anywhere it needs to, but if it’s a sortable column the data must be in the same table as the sort key.

    Hopefully that’s enough to get a one row table displayed, you can build from there.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Trouble with WP_List_Table’ is closed to new replies.