Skip to content

Instantly share code, notes, and snippets.

@sourovroy
Last active September 26, 2017 13:32
Show Gist options
  • Select an option

  • Save sourovroy/10191e161953927d0eb56168b87bc84a to your computer and use it in GitHub Desktop.

Select an option

Save sourovroy/10191e161953927d0eb56168b87bc84a to your computer and use it in GitHub Desktop.
Custom pagination for WordPress admin panel
<?php
function affilate_list_pagination($total_items, $total_pages, $current){
$output = '<span class="displaying-num">'.sprintf(_n('%s item', '%s items', $total_items), number_format_i18n($total_items)).'</span>';
$removable_query_args = wp_removable_query_args();
$current_url = set_url_scheme('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$current_url = remove_query_arg($removable_query_args, $current_url);
$page_links = array();
$total_pages_before = '<span class="paging-input">';
$total_pages_after = '</span></span>';
$disable_first = $disable_last = $disable_prev = $disable_next = false;
if( $current == 1 ){
$disable_first = true;
$disable_prev = true;
}
if( $current == 2 ){
$disable_first = true;
}
if( $current == $total_pages ){
$disable_last = true;
$disable_next = true;
}
if( $current == $total_pages - 1 ){
$disable_last = true;
}
if( $disable_first ){
$page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">&laquo;</span>';
}else{
$page_links[] = sprintf( "<a class='first-page' href='%s'><span aria-hidden='true'>%s</span></a>",
esc_url( remove_query_arg( 'paginate', $current_url ) ),
'&laquo;'
);
}
if( $disable_prev ){
$page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">&lsaquo;</span>';
}else{
$page_links[] = sprintf( "<a class='prev-page' href='%s'><span aria-hidden='true'>%s</span></a>",
esc_url( add_query_arg( 'paginate', max( 1, $current-1 ), $current_url ) ),
'&lsaquo;'
);
}
$html_current_page = sprintf( "<input class='current-page' id='current-page-selector' type='text' name='paginate' value='%s' size='%d' aria-describedby='table-paging' /><span class='tablenav-paging-text'>",
$current,
strlen( $total_pages )
);
$html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
$page_links[] = $total_pages_before . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . $total_pages_after;
if( $disable_next ){
$page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">&rsaquo;</span>';
}else{
$page_links[] = sprintf( "<a class='next-page' href='%s'><span aria-hidden='true'>%s</span></a>",
esc_url( add_query_arg( 'paginate', min( $total_pages, $current+1 ), $current_url ) ),
'&rsaquo;'
);
}
if ( $disable_last ) {
$page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">&raquo;</span>';
} else {
$page_links[] = sprintf( "<a class='last-page' href='%s'><span aria-hidden='true'>%s</span></a>",
esc_url( add_query_arg( 'paginate', $total_pages, $current_url ) ),
'&raquo;'
);
}
$output .= "\n<span class='pagination-links'>" . join( "\n", $page_links ) . '</span>';
if ( $total_pages ) {
$page_class = $total_pages < 2 ? ' one-page' : '';
} else {
$page_class = ' no-pages';
}
$_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
echo $_pagination;
}//End of affilate_list_pagination
$current = (isset($_GET['paginate']) && !empty($_GET['paginate'])) ? (int) $_GET['paginate'] : 1;
$total_pages = $post_query->max_num_pages;
$total_items = $post_query->found_posts;
affilate_list_pagination($total_items, $total_pages, $current);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment