article

Tuesday, November 29, 2016

Add pagination in wordpress admin in customized plugin

Add pagination in wordpress admin in customized plugin

Create folder name admin_menu path wp-content\plugins\admin_menu

Create php file name admin_menu path wp-content\plugins\admin_menu\admin_menu.php

admin_menu.php source code

<?php
/**
* Plugin Name: Admin Menu
* Plugin URI: https://tutorial101.blogspot.com/
* Description: A custom admin menu.
* Version: 1.0
* Author: Cairocoders
* Author URI: http://tutorial101.blogspot.com/
**/
function theme_options_panel(){
  add_menu_page('Theme page title', 'Theme menu', 'manage_options', 'theme-options', 'wps_theme_func');
  add_submenu_page('theme-options', 'Settings page title', 'Settings menu label', 'manage_options', 'theme-op-settings', 'wps_theme_func_settings');
  add_submenu_page('theme-options', 'FAQ page title', 'FAQ menu label', 'manage_options', 'theme-op-faq', 'wps_theme_func_faq');
  add_submenu_page('theme-options', 'My List Table', 'My List Table', 'manage_options', 'my-list-table', 'wps_theme_list_table');

}
add_action('admin_menu', 'theme_options_panel');

//function
function wps_theme_func(){
    echo '<div class="wrap">
 <div id="icon-users" class="icon32"></div>
 <h2>Theme Options</h2>
 </div>';
}
function wps_theme_func_settings(){
    echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div>
    <h2>Settings</h2></div>';
}
function wps_theme_func_faq(){
    echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div>
    <h2>FAQ</h2></div>';
}
function wps_theme_list_table(){
    echo '<div class="wrap">
 <div id="icon-users" class="icon32"></div>
 <h2>My List Table Test - Pagination</h2>
 </div>';
 global $wpdb;
 echo '<form method="get" action="" id="posts-filter">
  <table class="wp-list-table widefat fixed pages">
  <tbody id="the-list">
   <thead>
    <tr>
  <th class="manage-column column-cb check-column" id="cb" scope="col"></th>
  <th class="manage-column column-name" scope="col">User ID</th>
  <th class="manage-column column-name" scope="col">Invoice #</th>
  <th class="manage-column column-name" scope="col">Date Order</th>
  <th class="manage-column column-name" scope="col">Paid By</th>
  <th class="manage-column column-name" scope="col">Action</th>
    </tr>
   </thead>
   <tfoot>
    <tr>
  <th class="manage-column column-cb check-column" scope="col"></th>
  <th class="manage-column column-name" scope="col">User ID</th>
  <th class="manage-column column-name" scope="col">Invoice #</th>
  <th class="manage-column column-name" scope="col">Date Order</th>
  <th class="manage-column column-name" scope="col">Paid By</th>
  <th class="manage-column column-name" scope="col">Action</th>
    </tr>
   </tfoot>';
    $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
    $limit = 3; // number of rows in page
    $offset = ( $pagenum - 1 ) * $limit;
    $total = $wpdb->get_var( "SELECT COUNT('id') FROM wp_sdn_userorder" );
    $num_of_pages = ceil( $total / $limit );
    $sql_usersorder = $wpdb->get_results( "SELECT * FROM wp_sdn_userorder ORDER BY dateorder DESC LIMIT $offset, $limit");
    $totalrec =  $wpdb->num_rows;
    $isodd=array('','alternate');
    $n=0;
    foreach ($sql_usersorder as $rows_usersorder) 
    {
  $n++;
  $id = $rows_usersorder->id;   
  $user_id = $rows_usersorder->user_id;
  $course_invoice = $rows_usersorder->course_invoice;
  $dateorder = $rows_usersorder->dateorder;
  $paidby = $rows_usersorder->paidby;
  echo '<tr class="type-page '.$isodd[$n%2].'">
  <th class="check-column" scope="row"><center>'.$n.'</center></th> 
  <td class="column-title"><strong><a title="Edit" href="#" class="row-title">'.$user_id.''.rand().'</a></strong>
  <div class="row-actions">
  <span class="edit"><a title="Edit this item" href="#">Edit</a> | </span>
  <span class="trash"><a href="javascript:delusers('.$id.','.$user_id.')" title="Move this item to the Trash" class="submitdelete">Trash</a> | </span>
  </div>
    </td>
    <td>'.$course_invoice.'</td>
    <td>'.$dateorder.'</td>  
    <td>'.$paidby.'</td>
    <td><a href="#">View</a> | <a href="#">Delete</a></td>
  </tr>';
    }
   echo '</tbody>
  </table>
  <div class="tablenav bottom">
   <div class="alignleft actions bulkactions">';
    $page_links = paginate_links( array(
  'base' => add_query_arg( 'pagenum', '%#%' ),
  'format' => '',
  'prev_text' => __( '«', 'text-domain' ),
  'next_text' => __( '»', 'text-domain' ),
  'total' => $num_of_pages,
  'current' => $pagenum
    ) );
    if ( $page_links ) {
  echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
    }
   echo '</div>
   <br class="clear">
  </div>
 </form>';
}

Related Post