Advertisement
Guest User

Create Native Admin Tables In WordPress The Right Way

a guest
Dec 25th, 2012
1,433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.56 KB | None | 0 0
  1. <?php
  2. //Our class extends the WP_List_Table class, so we need to make sure that it's there
  3. if(!class_exists('WP_List_Table')){
  4.     require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  5. }
  6.  
  7. class Links_List_Table extends WP_List_Table {
  8.  
  9.     /**
  10.      * Constructor, we override the parent to pass our own arguments
  11.      * We usually focus on three parameters: singular and plural labels, as well as whether the class supports AJAX.
  12.      */
  13.      function __construct() {
  14.          parent::__construct( array(
  15.         'singular'=> 'wp_list_text_link', //Singular label
  16.         'plural' => 'wp_list_test_links', //plural label, also this well be one of the table css class
  17.         'ajax'  => false //We won't support Ajax for this table
  18.         ) );
  19.      }
  20.  
  21.     /**
  22.      * Add extra markup in the toolbars before or after the list
  23.      * @param string $which, helps you decide if you add the markup after (bottom) or before (top) the list
  24.      */
  25.     function extra_tablenav( $which ) {
  26.         if ( $which == "top" ){
  27.             //The code that goes before the table is here
  28.             echo"Hello, I'm before the table";
  29.         }
  30.         if ( $which == "bottom" ){
  31.             //The code that goes after the table is there
  32.             echo"Hi, I'm after the table";
  33.         }
  34.     }
  35.  
  36.     /**
  37.      * Define the columns that are going to be used in the table
  38.      * @return array $columns, the array of columns to use with the table
  39.      */
  40.     function get_columns() {
  41.         return $columns= array(
  42.             'col_link_id'=>__('ID'),
  43.             'col_link_name'=>__('Name'),
  44.             'col_link_url'=>__('Url'),
  45.             'col_link_description'=>__('Description'),
  46.             'col_link_visible'=>__('Visible')
  47.         );
  48.     }
  49.  
  50.     /**
  51.      * Decide which columns to activate the sorting functionality on
  52.      * @return array $sortable, the array of columns that can be sorted by the user
  53.      */
  54.     public function get_sortable_columns() {
  55.         return $sortable = array(
  56.             'col_link_id'=>array('link_id'),
  57.             'col_link_name'=>array('link_name'),
  58.             'col_link_visible'=>array('link_visible')
  59.         );
  60.     }
  61.  
  62.     /**
  63.      * Prepare the table with different parameters, pagination, columns and table elements
  64.      */
  65.     function prepare_items() {
  66.         global $wpdb, $_wp_column_headers;
  67.         $screen = get_current_screen();
  68.  
  69.         /* -- Preparing your query -- */
  70.         $query = "SELECT * FROM $wpdb->links";
  71.  
  72.         /* -- Ordering parameters -- */
  73.         //Parameters that are going to be used to order the result
  74.         $orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : 'ASC';
  75.         $order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : '';
  76.         if(!empty($orderby) & !empty($order)){ $query.=' ORDER BY '.$orderby.' '.$order; }
  77.  
  78.         /* -- Pagination parameters -- */
  79.         //Number of elements in your table?
  80.         $totalitems = $wpdb->query($query); //return the total number of affected rows
  81.         //How many to display per page?
  82.         $perpage = 5;
  83.         //Which page is this?
  84.         $paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : '';
  85.         //Page Number
  86.         if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
  87.         //How many pages do we have in total?
  88.         $totalpages = ceil($totalitems/$perpage);
  89.         //adjust the query to take pagination into account
  90.         if(!empty($paged) && !empty($perpage)){
  91.             $offset=($paged-1)*$perpage;
  92.             $query.=' LIMIT '.(int)$offset.','.(int)$perpage;
  93.         }
  94.  
  95.         /* -- Register the pagination -- */
  96.         $this->set_pagination_args( array(
  97.             "total_items" => $totalitems,
  98.             "total_pages" => $totalpages,
  99.             "per_page" => $perpage,
  100.         ) );
  101.         //The pagination links are automatically built according to those parameters
  102.        
  103.         /* — Register the Columns — */
  104.         $columns = $this->get_columns();
  105.         $hidden = array();
  106.         $sortable = $this->get_sortable_columns();
  107.         $this->_column_headers = array($columns, $hidden, $sortable);
  108.  
  109.         /* -- Fetch the items -- */
  110.         $this->items = $wpdb->get_results($query);
  111.     }
  112.  
  113.     /**
  114.      * Display the rows of records in the table
  115.      * @return string, echo the markup of the rows
  116.      */
  117.     function display_rows() {
  118.         //Get the records registered in the prepare_items method
  119.         $records = $this->items;
  120.  
  121.         //Get the columns registered in the get_columns and get_sortable_columns methods
  122.         list( $columns, $hidden ) = $this->get_column_info();
  123.  
  124.         //Loop for each record
  125.         if(!empty($records)){foreach($records as $rec){
  126.  
  127.             //Open the line
  128.             echo '<tr id="record_'.$rec->link_id.'">';
  129.             foreach ( $columns as $column_name => $column_display_name ) {
  130.  
  131.                 //Style attributes for each col
  132.                 $class = "class='$column_name column-$column_name'";
  133.                 $style = "";
  134.                 if ( in_array( $column_name, $hidden ) ) $style = ' style="display:none;"';
  135.                 $attributes = $class . $style;
  136.  
  137.                 //edit link
  138.                 $editlink  = '/wp-admin/link.php?action=edit&link_id='.(int)$rec->link_id;
  139.  
  140.                 //Display the cell
  141.                 switch ( $column_name ) {
  142.                     case "col_link_id": echo '<td '.$attributes.'>'.stripslashes($rec->link_id).'</td>';    break;
  143.                     case "col_link_name": echo '<td '.$attributes.'><strong><a href="'.$editlink.'" title="Edit">'.stripslashes($rec->link_name).'</a></strong></td>'; break;
  144.                     case "col_link_url": echo '<td '.$attributes.'>'.stripslashes($rec->link_url).'</td>'; break;
  145.                     case "col_link_description": echo '<td '.$attributes.'>'.$rec->link_description.'</td>'; break;
  146.                     case "col_link_visible": echo '<td '.$attributes.'>'.$rec->link_visible.'</td>'; break;
  147.                 }
  148.             }
  149.  
  150.             //Close the line
  151.             echo'</tr>';
  152.         }}
  153.     }
  154. }
  155.  
  156.  
  157.  
  158. //Prepare Table of elements
  159. $wp_list_table = new Links_List_Table();
  160. $wp_list_table->prepare_items();
  161.  
  162. //Table of elements
  163. $wp_list_table->display();
  164.  
  165.  
  166. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement