I Want a Pagination to My Options Page of Wordpress Plugin

I want a pagination to my options page of wordpress plugin?

The native way of doing this is extending the class WP_List_Table and let WordPress handle all the specifics of the table display. I know it from the plugin Internal Link Check, by kaiser. The example bellow is a stripped version that performs a very simple SQL query.

First, we need a helper page (using PHP5.3+ anonymous functions):

add_action('admin_menu', function() 
{
add_menu_page(
'TE',
'<span style="color:#e57300;">Table Example</span>',
'edit_pages',
'table-example',
function() {
echo '<div class="wrap">';
screen_icon('edit');
echo '<h2>Table Example</h2>';
new B5F_WP_Table();
echo '</div>';
},
'http://sstatic.net/stackexchange/img/favicon.ico',
1 // create before Dashboard menu item
);
});

This is the end result:

custom wp table

And here the class that performs everything (note the need of importing the main class). You'll have to adjust the query and the table columns for your data.

if( is_admin() && !class_exists( 'WP_List_Table' ) )
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );

class B5F_WP_Table extends WP_List_Table
{
private $order;
private $orderby;
private $posts_per_page = 5;

public function __construct()
{
parent :: __construct( array(
'singular' => 'table example',
'plural' => 'table examples',
'ajax' => true
) );
$this->set_order();
$this->set_orderby();
$this->prepare_items();
$this->display();
}

private function get_sql_results()
{
global $wpdb;
$args = array( 'ID', 'post_title', 'post_date', 'post_content', 'post_type' );
$sql_select = implode( ', ', $args );
$sql_results = $wpdb->get_results("
SELECT $sql_select
FROM $wpdb->posts
WHERE post_status = 'publish'
ORDER BY $this->orderby $this->order "
);
return $sql_results;
}

public function set_order()
{
$order = 'DESC';
if ( isset( $_GET['order'] ) AND $_GET['order'] )
$order = $_GET['order'];
$this->order = esc_sql( $order );
}

public function set_orderby()
{
$orderby = 'post_date';
if ( isset( $_GET['orderby'] ) AND $_GET['orderby'] )
$orderby = $_GET['orderby'];
$this->orderby = esc_sql( $orderby );
}

/**
* @see WP_List_Table::ajax_user_can()
*/
public function ajax_user_can()
{
return current_user_can( 'edit_posts' );
}

/**
* @see WP_List_Table::no_items()
*/
public function no_items()
{
_e( 'No posts found.' );
}

/**
* @see WP_List_Table::get_views()
*/
public function get_views()
{
return array();
}

/**
* @see WP_List_Table::get_columns()
*/
public function get_columns()
{
$columns = array(
'ID' => __( 'ID' ),
'post_title' => __( 'Title' ),
'post_date' => __( 'Date' ),
'post_type' => __( 'Type' )
);
return $columns;
}

/**
* @see WP_List_Table::get_sortable_columns()
*/
public function get_sortable_columns()
{
$sortable = array(
'ID' => array( 'ID', true ),
'post_title' => array( 'post_title', true ),
'post_date' => array( 'post_date', true )
);
return $sortable;
}

/**
* Prepare data for display
* @see WP_List_Table::prepare_items()
*/
public function prepare_items()
{
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array(
$columns,
$hidden,
$sortable
);

// SQL results
$posts = $this->get_sql_results();
empty( $posts ) AND $posts = array();

# >>>> Pagination
$per_page = $this->posts_per_page;
$current_page = $this->get_pagenum();
$total_items = count( $posts );
$this->set_pagination_args( array (
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil( $total_items / $per_page )
) );
$last_post = $current_page * $per_page;
$first_post = $last_post - $per_page + 1;
$last_post > $total_items AND $last_post = $total_items;

// Setup the range of keys/indizes that contain
// the posts on the currently displayed page(d).
// Flip keys with values as the range outputs the range in the values.
$range = array_flip( range( $first_post - 1, $last_post - 1, 1 ) );

// Filter out the posts we're not displaying on the current page.
$posts_array = array_intersect_key( $posts, $range );
# <<<< Pagination

// Prepare the data
$permalink = __( 'Edit:' );
foreach ( $posts_array as $key => $post )
{
$link = get_edit_post_link( $post->ID );
$no_title = __( 'No title set' );
$title = ! $post->post_title ? "<em>{$no_title}</em>" : $post->post_title;
$posts[ $key ]->post_title = "<a title='{$permalink} {$title}' href='{$link}'>{$title}</a>";
}
$this->items = $posts_array;
}

/**
* A single column
*/
public function column_default( $item, $column_name )
{
return $item->$column_name;
}

/**
* Override of table nav to avoid breaking with bulk actions & according nonce field
*/
public function display_tablenav( $which ) {
?>
<div class="tablenav <?php echo esc_attr( $which ); ?>">
<!--
<div class="alignleft actions">
<?php # $this->bulk_actions( $which ); ?>
</div>
-->
<?php
$this->extra_tablenav( $which );
$this->pagination( $which );
?>
<br class="clear" />
</div>
<?php
}

/**
* Disables the views for 'side' context as there's not enough free space in the UI
* Only displays them on screen/browser refresh. Else we'd have to do this via an AJAX DB update.
*
* @see WP_List_Table::extra_tablenav()
*/
public function extra_tablenav( $which )
{
global $wp_meta_boxes;
$views = $this->get_views();
if ( empty( $views ) )
return;

$this->views();
}
}

Helper plugin to style the admin: WordPress Admin Style, by bueltge

Add pagination in wordpress admin in my own customized plugin

I solved this problem in different way. And solution is here

I use second option which has title "How To Add Pagination To WordPress Plugin?".
This gives almost same pagination functionality as WordPress has.

Do not forget to download that "pagination.class.php" file, put it in your plugin folder and give appropriate path while including this file in your plugin.

How to add Pagination in custom plugin list page

Try this code.

<?php

function wp_schools_list() {
global $wpdb;
$table_name = $wpdb->prefix . "school";

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

$limit = 10; // number of rows in page
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var( "select count(*) as total from $table_name" );
$num_of_pages = ceil( $total / $limit );

$rows = $wpdb->get_results( "SELECT id,code,name,image_name from $table_name limit $offset, $limit" );
$rowcount = $wpdb->num_rows;

?>
<link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>mywp/sinetiks-schools/style-admin.css" rel="stylesheet" />

<div class="wrap abs">
<h2>Schools</h2>
<div class="tablenav top">
<div class="alignleft actions">
<a href="<?php echo admin_url('admin.php?page=mywp_schools_create'); ?>">Add New</a>
</div>
<br class="clear">
</div>
<?php
$path_array = wp_upload_dir()['baseurl']; // wp_upload_dir has diffrent types of array I am used 'baseurl' for path

?>
<table class='wp-list-table widefat fixed striped posts'>
<tr>
<th class="manage-column ss-list-width">ID</th>
<th class="manage-column ss-list-width">Code</th>
<th class="manage-column ss-list-width">Name</th>
<th class="manage-column ss-list-width">Image</th>
<th> </th>
<th> </th>
</tr>
<?php
if($rowcount>0){
foreach ($rows as $row) { ?>
<tr>
<td class="manage-column ss-list-width"><?php echo $row->id; ?></td>
<td class="manage-column ss-list-width"><?php echo $row->code; ?></td>
<td class="manage-column ss-list-width"><?php echo $row->name; ?></td>

<td><img src="<?php echo $path_array; ?>/2016/12/<?php echo $row->image_name;?> " width="100" height="100" /> </td>
<td><a href="<?php echo admin_url('admin.php?page=wp_schools_update&id=' . $row->id); ?>">Update</a></td>
<td><a href="<?php echo admin_url('admin.php?page=wp_schools_delete&id=' . $row->id); ?>">Delete</a></td>

</tr>
<?php } }else{
echo "<tr><td cols=an='5'>No records found</td></tr>";
} ?>
</table>
</div>
<?php

$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" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
}

Updated

Pagination on static front page - WordPress

Without any plugin you can use this (just a simple example)

$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args=array('category_name'=>'portfolio','posts_per_page'=>4,'paged'=>$paged);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
/...
endwhile;
posts_nav_link();
wp_reset_query();
endif;

Visit Codex to know more about formating links.

Alternatively you can use plugins like pagenavi (I use this) or infinite scroll. There is also a nice tutorial if you manually want to build your infinite scroll.

How to add pagination for a page of wordpress

It looks like currently woocommerce plugin does not actually support pagination on a products page.

See here http://ideas.woothemes.com/forums/133476-woocommerce/suggestions/4146798-add-pagination-support-for-list-of-products-render

There seems to be two common work arounds.

A: In your wordpress settings. Change the max amount of posts visible on a page and therefore all products are shown, hence no more need for pagination. Alternatively of course you could also set your per_page attribute to some ridiculous number.

B: Download the plugin wp-navi. Activate it and configure it. Then add the following code to your functions.php in your theme folder.

remove_action('woocommerce_pagination', 'woocommerce_pagination', 10);
function woocommerce_pagination() {
wp_pagenavi();
}
add_action( 'woocommerce_pagination', 'woocommerce_pagination', 10);

Unfortunately there seems to be no definitive answer for this at the moement.

how add pagination to author page in wordpress

I use this plug in wordpress hope it help you.

Try this plugin

Wordpress paginate_links select option

I have created an additional plugin for myself that generates either a

  1. Normal pagination [1, 2, 3, 4, 5]

  2. Select option <<NEXT [SELECT] PREV>>

Change the config.php file and call the function (rename it if you would like)

<?php jagmit_paging_nav(); ?>

https://github.com/jagmitg/Wordpress-Select-Pagination

custom Wordpress pagination for a specific table

You should be using custom post types instead of building custom tables.

That way you can use wp at its full extend and make use of all its options in post types.

You can read about it here https://codex.wordpress.org/Post_Types

Or you can use the Custom Post Types UI plugin https://wordpress.org/plugins/custom-post-type-ui/



Related Topics



Leave a reply



Submit