0

enter image description here

As the image above, my custom wp_list_table didn't show correctly. Code as below

<?php
if ( !class_exists( 'V2OMLVG_Video_Table' ) )
{
    class V2OMLVG_Video_Table extends WP_List_Table
    {
        function __construct ()
        {
            parent :: __construct (
                array(
                    'singular'  => 'wp_list_video',
                    'plural'    => 'wp_list_videos',
                    'ajax'      => false
                )
            );
        }

        function get_columns()
        {
            return $columns = array(
                'cb'                        => '<input type="checkbox" />',
                'col_id'                    => __('ID'),
                'col_video_title'           => __('Video Title'),
                'col_video_description'     => __('Video Description'),
                'col_created_time'          => __('Created'),
                'col_updated_time'          => __('Updated'),
                'col_last_modified_user'    => __('Last Modified User'),
                'col_video_type'            => __('Video Platform'),
            );
        }

        function column_cb($item) {
            return sprintf(
                '<input type="checkbox" name="id[]" value="%s" />', $item['id']
            );    
        }

        function col_video_title ($item, $column_name)
        {

            var_dump($item);
            $actions = array(
                'edit'      => sprintf('<a href="?page=%s&id=%s">Edit</a>', 'v2omlvg_edit_video', $item['ID']),
                'delete'    => sprintf('<a href="?page=%s&action=%s&book=%s">Delete</a>', 'v2omlvg_edit_video', 'delete', $item['ID']),
            );

            return sprintf('%1$s %2$s', $item['video_title'], $this->row_actions($actions) );
        }

        function get_bulk_actions() {
            $actions = array(
                'delete'    => 'Delete'
            );
            return $actions;
        }

        public function get_sortable_columns ()
        {
            return $sortable = array(
                'col_video_title'           => array('video_title', false),
                'col_created_time'          => array('created_time', false),
                'col_updated_time'          => array('updated_time', false),
                'col_video_type'            => array('video_type', false)
            );
        }

        function prepare_items ()
        {
            global $wpdb, $_wp_column_headers;
            $screen = get_current_screen();

            $table_name = $wpdb -> prefix . 'v2omlvg_video_gallery';

            /* -- Preparing your query -- */
            $query = "SELECT * FROM {$table_name}";

            $where = !empty( $_GET['v_type'] ) ? " WHERE video_type='" . mysql_real_escape_string( $_GET["v_type"] ) . "'" : "";
            $query .= $where;
            /* -- Ordering parameters -- */
            //Parameters that are going to be used to order the result
            $orderby    = !empty( $_GET["orderby"] ) ? mysql_real_escape_string( $_GET["orderby"] ) : 'updated_time';
            $order      = !empty( $_GET["order"] ) ? mysql_real_escape_string( $_GET["order"] ) : 'ASC';
            if ( !empty( $orderby ) & !empty( $order ) )
                $query .= ' ORDER BY ' . $orderby. ' ' . $order;

            /* -- Pagination parameters -- */
            //Number of elements in your table?
            $totalitems = $wpdb -> query ( $query ); //return the total number of affected rows
            //How many to display per page?
            $perpage = 20;
            //Which page is this?
            $paged = !empty( $_GET["paged"] ) ? mysql_real_escape_string( $_GET["paged"] ) : '';
            //Page Number
            if( empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
            //How many pages do we have in total?
            $totalpages = ceil($totalitems/$perpage);
            //adjust the query to take pagination into account
            if( !empty($paged) && !empty($perpage) ){
                $offset = ($paged - 1) * $perpage;
                $query.=' LIMIT ' . (int) $offset . ',' . (int) $perpage;
            }

            /* -- Register the pagination -- */
            $this->set_pagination_args( array(
                "total_items" => $totalitems,
                "total_pages" => $totalpages,
                "per_page" => $perpage,
            ) );
            //The pagination links are automatically built according to those parameters

            /* -- Register the Columns -- */
            $columns = $this->get_columns();
            $hidden = array();
            $sortable = $this->get_sortable_columns();
            $this->_column_headers = array($columns, $hidden, $sortable);

            /* -- Fetch the items -- */
            $this->items = $wpdb->get_results($query);
            // echo $query;
        }

        function display_rows() {

            //Get the records registered in the prepare_items method
            $records = $this->items;

            //Get the columns registered in the get_columns and get_sortable_columns methods
            list( $columns, $hidden ) = $this->get_column_info();

            //Loop for each record
            if(!empty($records)){
                foreach($records as $rec){

                    //Open the line
                    echo '<tr id="record_'.$rec->id.'">';
                    foreach ( $columns as $column_name => $column_display_name ) {
                        //Style attributes for each col
                        $class = "class='$column_name column-$column_name'";
                        $style = "";
                        if ( in_array( $column_name, $hidden ) ) $style = ' style="display:none;"';
                        $attributes = $class . $style;


                        $wp_userdata = get_userdata( $rec->user_id );

                        $video_desc = $rec->video_description;

                        if( strlen ( $video_desc ) >= 50 )
                             $video_desc = substr($video_desc, 0, 50) . '...';

                        //Display the cell
                        switch ( $column_name ) {
                            case 'col_id':  echo '<td '.$attributes.'>'.stripslashes($rec->id).'</td>';   break;
                            case 'col_video_title': echo '<td '.$attributes.'>'.stripslashes($rec->video_title).'</td>'; break;
                            case 'col_video_description': echo '<td '.$attributes.'>'.stripslashes($video_desc).'</td>'; break;
                            case 'col_created_time': echo '<td '.$attributes.'>'.$rec->created_time.'</td>'; break;
                            case 'col_updated_time': echo '<td '.$attributes.'>'.$rec->updated_time.'</td>'; break;
                            case 'col_last_modified_user': echo '<td '.$attributes.'>'.$wp_userdata->user_login.'</td>'; break;
                            case 'col_video_type': echo '<td '.$attributes.'>'.$rec->video_type.'</td>'; break;
                        }
                    }

                    //Close the line
                    echo'</tr>';
                }
            }
        }
    }
}
?>
2
  • Can you explain in words, what the problem is? It's not obvious by just looking at the screenshot.
    – birgire
    May 14, 2014 at 16:26
  • Checkbox missing and Edit | Delete Link cannot show. I think is column_$custom function cannot work.
    – garydefty
    May 15, 2014 at 1:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.