Need Websites?

We, QuickBizTech have 8 Years of Exp in Web development in PHP and hosting. Skills: Photoshop, Designing, Core PHP, MySql, Joomla, Wordpress, Drupal, Magento, phpBB, Opencart, Smarty, Google API, JQuery, Charts, oAuth, SEO, Payment Gateways.


Please contact us for any kind of websites to be developed, upgraded, migrated. Reach our team for your dream website @QuickBizTech

Thursday, October 3, 2013

Add pagination in wordpress admin in my own customized plugin

Easy Steps :

$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;

Find total numbers of records

$limit = 10; // number of rows in page
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var( "SELECT COUNT(`id`) FROM {$wpdb->prefix}table_name" );
$num_of_pages = ceil( $total / $limit );

Give limit:

$entries = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}table_name LIMIT $offset, $limit" );

Add this code where you want pagination:

$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '«', 'aag' ),
    'next_text' => __( '»', 'aag' ),
    'total' => $total,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}

No comments:

Post a Comment