Simple pagination system in your WordPress plugins


May be you are developing a plugin or a theme and it has something to do with handing some pagination of your custom tables data. Here’s how we can simply manage it, I am not going to build all the admin panel stuffs, but just showing you the process.

Step 1:

Lets get the page number from the url query string. If we don’t find anything, we’ll set the page number to 1.

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

Step 2:

Now we need to set per page entry limit and the page offset. We will use the limit and offset to get data from our MySQL query. If you are confused about the offset, may be you’ve seen it on PHPMyAdmin like this: "SELECT * FROM `table_name` LIMIT 0, 10". Here, we are getting the first 10 entries from our database. If we want get the next 10 entries, our limit will be "LIMIT 10, 10". So the first digit for the limit is the offset. It tells us from where we’ll get our next entries.

$limit = 10;
$offset = ( $pagenum - 1 ) * $limit;

So, by this two lines we are setting the limit and our offset dynamically.

Step 3:

Now we need to get the entries from our database table with the help of our limit and offset variable and need to show them as you like, may be in a table.

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

So, now if you change the url “pagenum” paramater from your browser, you can see that we are getting the entries by our page number. So it’s like we have completed our pagination system :D

Step 4:

As we have completed the pagination system working, we just need to show the pagination links now and we’ll be done. WordPress gives us a nice function "paginate_links" to generate a cool pagination navigation link.
At first, we need to get the count of total number of entries in our table and calculate how many pages will need to show the whole entries.

$total = $wpdb->get_var( "SELECT COUNT(`id`) FROM {$wpdb->prefix}table_name" );
$num_of_pages = ceil( $total / $limit );

Now, we can use that nifty function paginate_links to generate our pagination links.

$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>';
}

So, we are passing our current page number and total number of pages to that function and we are getting a nice and cool pagination system.

Here’s a sample code for generating a table with data from your posts table.

<?php
global $wpdb;
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
$limit = 5;

$offset = ( $pagenum - 1 ) * $limit;
$entries = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts LIMIT $offset, $limit" );
echo '<div class="wrap">';
?>

<table class="widefat">
    <thead>
        <tr>
            <th scope="col" class="manage-column column-name" style="" >Post Title</th>
            <th scope="col" class="manage-column column-name" style="">Date</th>
        </tr>
    </thead>

    <tfoot>
        <tr>
            <th scope="col" class="manage-column column-name" style="" >Post Title</th>
            <th scope="col" class="manage-column column-name" style="">Date</th>
        </tr>
    </tfoot>

    <tbody>
        <?php if( $entries ) { ?>

            <?php
            $count = 1;
            $class = '';
            foreach( $entries as $entry ) {
                $class = ( $count % 2 == 0 ) ? ' class="alternate"' : '' ;
            ?>

            <tr<?php echo $class; ?>>
                <td><?php echo $entry->post_title; ?></td>
                <td><?php echo $entry->post_date; ?></td>
            </tr>

            <?php
                 $count++;
             }
            ?>

        <?php } else { ?>
        <tr>
            <td colspan="2" >No posts yet</td>
        </tr>
        <?php } ?>
    </tbody>
</table>

<?php

$total = $wpdb->get_var( "SELECT COUNT(`id`) FROM {$wpdb->prefix}posts" );
$num_of_pages = ceil( $total / $limit );
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'aag' ),
    'next_text' => __( '&raquo;', 'aag' ),
    '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>';

?>

Hope this solution will help you.

Leave a comment