Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. <?php
  2.  
  3. add_action('admin_menu', 'add_example_menues');
  4.  
  5. function add_example_menues() {
  6. add_menu_page('test', 'test', 'administrator', 'test-top', 'build_test_page');
  7. add_submenu_page('test-top', 'All tests', 'All tests', 'administrator', 'test-top');
  8. }
  9.  
  10. if(!class_exists('WP_List_Table')){
  11. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  12. }
  13.  
  14. class test_List_Table extends WP_List_Table {
  15. function __construct() {
  16. parent::__construct( array(
  17. 'singular' => 'test',
  18. 'plural' => 'tests',
  19. 'ajax' => false
  20. ));
  21. }
  22.  
  23.  
  24. function extra_tablenav ($which) {
  25. if ($which == "top") {
  26. echo "Top";
  27. }
  28. if ($which == "bottom") {
  29. echo "Bottom";
  30. }
  31. }
  32.  
  33. function get_columns() {
  34. $columns = array(
  35. 'id' => 'ID',
  36. 'title' => 'Title',
  37. 'user_id' => 'User ID',
  38. 'description' => 'Description'
  39. );
  40. return $columns;
  41. }
  42.  
  43. function get_sortable_columns() {
  44. return $sortable = array(
  45. 'id' => array('id',false),
  46. 'title' => array('title',false),
  47. 'user_id' => array('user_id',false)
  48. );
  49. }
  50.  
  51. function prepare_items() {
  52. global $wpdb;
  53.  
  54. //data normally gotten from non-wp database. Wordpress db user has access, as per this SE post:
  55. //http://wordpress.stackexchange.com/questions/1604/using-wpdb-to-connect-to-a-separate-database
  56. $query = "SELECT `id`, `title`, `user_id`, `description` FROM otherdb.example_table";
  57.  
  58. //pagination stuff
  59. $orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : 'ASC';
  60. $order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : '';
  61. if(!empty($orderby) & !empty($order)){ $query.=' ORDER BY '.$orderby.' '.$order; }
  62. $totalitems = $wpdb->query($query);
  63. echo "$totalitems";
  64. $per_page = 5;
  65. $paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : '';
  66. if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
  67. $totalpages = ceil($totalitems/$perpage);
  68. if(!empty($paged) && !empty($perpage)){
  69. $offset=($paged-1)*$perpage;
  70. $query.=' LIMIT '.(int)$offset.','.(int)$perpage;
  71. }
  72. $this->set_pagination_args( array(
  73. "total_items" => 4,
  74. "total_pages" => 1,
  75. "per_page" => 5,
  76. ) );
  77.  
  78.  
  79. $columns = $this->get_columns();
  80. $hidden = array();
  81. $sortable = $this->get_sortable_columns();
  82.  
  83. $this->_column_headers = array($columns, $hidden, $sortable);
  84.  
  85. //actual data gotten from database, but for SE use hardcoded array
  86. //$data = $wpdb->get_results($query, ARRAY_N);
  87.  
  88. $example_data = array(
  89. array(
  90. 'id' => 1,
  91. 'title' => 'nonsense',
  92. 'user_id' => 1,
  93. 'description' => 'asdf'
  94. ),
  95. array(
  96. 'id' => 2,
  97. 'title' => 'notanumber',
  98. 'user_id' => 2,
  99. 'description' => '404'
  100. ),
  101. array(
  102. 'id' => 3,
  103. 'title' => 'I Am A Title',
  104. 'user_id' => 3,
  105. 'description' => 'desc'
  106. ),
  107. array(
  108. 'id' => 4,
  109. 'title' => 'Example',
  110. 'user_id' => 4,
  111. 'description' => 'useless'
  112. ),
  113. array(
  114. 'id' => 5,
  115. 'title' => 'aeou',
  116. 'user_id' => 5,
  117. 'description' => 'keyboard layouts'
  118. ),
  119. array(
  120. 'id' => 6,
  121. 'title' => 'example data',
  122. 'user_id' => 6,
  123. 'description' => 'data example'
  124. ),
  125. array(
  126. 'id' => 7,
  127. 'title' => 'last one',
  128. 'user_id' => 7,
  129. 'description' => 'done'
  130. )
  131. );
  132.  
  133.  
  134. //This is the line:
  135. $this->items = $example_data;
  136. //When the above line is commented, it works as expected (except for the lack of data, of course)
  137. }
  138. }
  139.  
  140.  
  141. function build_test_page() {
  142.  
  143. $testListTable = new test_List_Table();
  144. $testListTable->prepare_items();
  145.  
  146. ?>
  147. <div class="wrap">
  148.  
  149. <div id="icon-users" class="icon32"><br/></div>
  150. <h2>List Table Test</h2>
  151.  
  152. <?php $testListTable->display() ?>
  153.  
  154. </div>
  155. <?php
  156. }
  157. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement