WordPress Wordpress Tutorials

WORDPRESS LISTS OF USERS WITH PAGINATION

Wordpress Themes and Plugins

In this documentation we will generate the lists of users with pagination. We will be using get_users($args) function this will retrieve all users in array base on the arguments given. For more information about this function please refer to WordPress Codex.

Example usage of get_users function

$users    = get_users(array('role' => 'agent')); //get all the lists of users with the role "agent" 

DISPLAY THE LISTS OF USERS

In this example we will generate the lists of users with the role agent.

$args = array(
'offset' => $offset,
'number' => $number,
'role' => 'agent',
'fields' => array( 'ID', 'user_login', 'user_email' )
);
$query    = get_users($args);
 
if(is_array($query)){
     foreach($query as $agent_data) {
        echo '<tr>';
        echo '<td>'.$agent_data->user_login.'</td>';
        echo '<td>'.$agent_data->user_email.'</td>';
        echo '<td><a href="https://www.gta-link.com/support/edit-agent/?uid='.$agent_data->ID.'">Edit</a></td>';
        echo '</tr>';
     }
}

ADDING PAGINATION

We need to add a pagination because we don’t want to display thousand of users on one page. That could crush a website for loading. So we need to split the results in pages. We will be using paginate_links function in WordPress.

 
<?php
 
$number = 3; //max display per page
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; //current number of page
$offset = ($paged - 1) * $number; //page offset
$users = get_users(array('role' => 'agent')); //get all the lists of users
 
$args = array(
     'offset' => $offset,
     'number' => $number,
     'role' => 'agent',
     'fields' => array( 'ID', 'user_login', 'user_email' )
);
$query = get_users($args);//query the maximum users that we will be displaying
 
$total_users = count($users);//count total users
$total_query = count($query);//count the maximum displayed users
 
$total_pages = ($total_users / $number); // get the total pages by dividing the total users to the maximum numbers of user to be displayed
 
//Check if the total pages has a decimal we will add + 1 page
$total_pages = is_float($total_pages) ? intval($total_users / $number) + 1 : intval($total_users / $number);
 
if ($total_users > $total_query) {
echo '<div id="support-pagination" class="clearfix">';
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%/',
'current' => $current_page,
'total' => $total_pages,
'prev_next'    => false,
'type'         => 'list',
));
}
echo '</div>';
?> 

All code togheter for list WordPress user with pagination

<h4>List of Agents</h4>
<table>
   <tr>
     <th>Agent ID</th>
     <th>Email</th>
     <th>Action</th>
</tr>
<?php
 
$number = 3; //max display per page
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; //current number of page
$offset = ($paged - 1) * $number; //page offset
$users = get_users(array('role' => 'agent')); //get all the lists of users
$args = array(
     'offset' => $offset,
     'number' => $number,
     'role' => 'agent',
     'fields' => array( 'ID', 'user_login', 'user_email' )
);
$query = get_users($args);//query the maximum users that we will be displaying
$total_users = count($users);//count total users
$total_query = count($query);//count the maximum displayed users
$total_pages = ($total_users / $number); // get the total pages by dividing the total users to the maximum numbers of user to be displayed //Check if the total pages has a decimal we will add + 1 page
$total_pages = is_float($total_pages) ? intval($total_users / $number) + 1 : intval($total_users / $number);

if(is_array($query )){
     foreach($query as $agent_data) {
        echo '<tr>';
        echo '<td>'.$agent_data->user_login.'</td>';
        echo '<td>'.$agent_data->user_email.'</td>';
        echo '<td><a href="https://www.gta-link.com/support/edit-agent/?uid='.$agent_data->ID.'">Edit</a></td>';
        echo '</tr>';
     }
}
?>
</table>

<?php
if ($total_users > $total_query) {
    echo '<div id="support-pagination" class="clearfix">';
    $current_page = max(1, get_query_var('paged'));
    echo paginate_links(array(
       'base' => get_pagenum_link(1) . '%_%',
       'format' => 'page/%#%/',
       'current' => $current_page,
       'total' => $total_pages,
       'prev_next' => false,
       'type' => 'list',
    ));
}
echo '</div>';
?>
</div>


Comments   

Leave a Comment

You may also like

loading...