wordpress custom function to create table using `WP_List_Table`?

I am new to WordPress. I am trying to create a WordPress table using WP_List_Table class. I created a table but it takes a long time. So, I want to create a function that allows me to create a WordPress table, where I can pass data and column array to the function and that function will, then, create the required WordPress table. I want to create table with edit, delete and sort-able functionality.

Related posts

Leave a Reply

1 comment

  1. hey try this code it dynamic function but you need to pass first argument kay and name is id.

    this is my class that is dynamic create the WP_List_table.

    <?php
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     * Description of wplist_table
     *
     * @author renishkhunt
     */
    if (!class_exists('WP_List_Table')) {
        require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
    }
    
    class wplist_table extends WP_List_Table
    {
    
        //put your code here
        var $data = array();
        var $default_columns = array();
    
        public function wplist_table($datad, $columns)
        {
            parent::__construct();
    
            $this->data = $datad;
            $this->default_columns = $columns;
        }
    
        function get_columns()
        {
    
            return $this->default_columns;
        }
    
        function prepare_items()
        {
            $columns = $this->get_columns();
            $hidden = array();
            $sortable = $this->get_sortable_columns();
            $this->_column_headers = array($columns, $hidden, $sortable);
            usort($this->data, array(&$this, 'usort_recorder'));
            $per_page = 10;
            $current_page = $this->get_pagenum();
            $total_items = count($this->data);
    
            // only ncessary because we have sample data
            $this->found_data = array_slice($this->data, (($current_page - 1) * $per_page), $per_page);
    
            $this->set_pagination_args(array(
                'total_items' => $total_items, //WE have to calculate the total number of items
                'per_page' => $per_page                     //WE have to determine how many items to show on a page
            ));
            $this->items = $this->found_data;
        }
    
        function column_default($item, $column_name)
        {
            foreach ($this->default_columns as $keys => $values) {
                if ($values == $column_name) {
                    if(isset($item[$column_name])){
                        return $item[$column_name];
                    }
                }
            }
        }
    
        function get_sortable_columns()
        {
    
            $i=0;
            $sortables = array();
            foreach ($this->default_columns as $keys => $values) {
    
                if($i == 0){
                    $i++;
                    //continue;
                }
                    $sortables[$keys] = array($values,false);
            }
            return $sortables;
        }
    
        function usort_recorder($a, $b)
        {
            $orderby = (!empty($_GET['orderby'])) ? $_GET['orderby'] : 'id';
    
            $order = (!empty($_GET['order'])) ? $_GET['order'] : 'asc';
    
            $resutl = strcmp($a[$orderby], $b[$orderby]);
            return ( $order === 'asc') ? $resutl : -$resutl;
        }
    
        function column_Name($item)
        {
            $action = array(
                'edit' => sprintf('<a href="?page=%s&action=%s&fields=%s">Edit</a>', $_REQUEST['page'], 'edit', $item['id']),
                'delete' => sprintf('<a href="?page=%s&action=%s&fields=%s">Delete</a>', $_REQUEST['page'], 'delete', $item['id'])
            );
            return sprintf('%1$s %2$s', $item['name'], $this->row_actions($action));
        }
    
        function get_bulk_action()
        {
            $actions = array(
                'delete' => 'Delete '
            );
            return $actions;
        }
    
        function column_db($item)
        {
            return sprintf("<input type='checkbox' name='id[]' value='%s'", $item['id']);
        }
    
    }
    
    ?>
    

    just you copy that code in file and pass arguments like column name and data like this.

      $data = array(
        array("id" => 1, "name" => "Renish Khunt", "add" => "asd"),
        array("id" => 2, "name" => "Renish Khunt", "add" => "asd"),
        array("id" => 3, "name" => "Renish Khunt", "add" => "asd")
    );
    $columns = array(
        "name" => "name",
        "add" => "add"
    );
    

    then after create the class object and pass the two arguments the data and column name like this.

    $mylist_table = new wplist_table($data, $columns);
    echo '<div class="wrap"><h2>Custome Fields<a class="add-new-h2" href="?page=' . $_REQUEST['page'] . '&action=add">Add New</a></h2>';
    $mylist_table->prepare_items();
    $mylist_table->display();
    echo "</div>";
    

    i hope this is used full for you that is the dynamic class you need to display more column in $column array add column name and $data array add that name of column as key or value like this.

    $data = array(
    array("id" => 1, "name" => "Renish Khunt", "add" => "asd","newcolumn"=>"value"),
    array("id" => 2, "name" => "Renish Khunt", "add" => "asd","newcolumn"=>"value"),
    array("id" => 3, "name" => "Renish Khunt", "add" => "asd","newcolumn"=>"value")
    );
    $columns = array(
        "name" => "name",
        "add" => "add",
        "newcolumn"=>"New Column"
    );
    

    like this i hope this code is used full for you.

    thank you.