I created this plugin to display a list of registered users with the Faciliator role. Displays the list in a table with name, country and avatar as titles. It works fine but the search box only considers the visible rows not the hidden pages due to pagination. Any help appreciated. This is the code.
<?php
function enqueue_plugin_scripts() {
wp_enqueue_style('facilitator-users-style', plugins_url('css/style.css', __FILE__));}
add_action('wp_enqueue_scripts', 'enqueue_plugin_scripts');
function display_facilitator_users_table() {
ob_start();
$users_per_page = 50;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset = ($paged - 1) * $users_per_page;
$args_all = array(
'role' => 'facilitator',
);
$all_facilitator_query = new WP_User_Query($args_all);
$args = array(
'role' => 'facilitator',
'number' => $users_per_page,
'offset' => $offset,
);
$facilitator_query = new WP_User_Query($args);
echo '<input type="text" id="user-search" placeholder="Search by name or country">';
echo '<table id="facilitator-users-table">';
echo '<thead><tr><th>Avatar</th><th>Name</th><th>Country</th></tr></thead>';
echo '<tbody>';
if (!empty($facilitator_query->results)) {
foreach ($facilitator_query->results as $user) {
$user_id = $user->ID;
$user_info = get_userdata($user_id);
$avatar = get_avatar($user_id, 60);
$name = $user_info->display_name;
$country = get_user_meta($user_id, 'user_country', true);
echo '<tr class="user-row ' . esc_attr($country) . '">';
echo '<td>' . $avatar . '</td>';
echo '<td><a href="' . get_author_posts_url($user_id) . '">' . esc_html($name) . '</a></td>';
echo '<td>' . esc_html($country) . '</td>';
echo '</tr>';
}
} else {
echo '<tr><td colspan="3">No facilitators found.</td></tr>';
}
echo '</tbody>';
echo '</table>';
echo '<div class="pagination">';
echo paginate_links(array(
'total' => ceil(count($all_facilitator_query->results) / $users_per_page),
'current' => $paged,
'prev_text' => '« Previous',
'next_text' => 'Next »',
));
echo '</div>';
return ob_get_clean();
}
add_shortcode('facilitator_users', 'display_facilitator_users_table');
function plugin_custom_script() {
?>
<script type="text/javascript">
jQuery(document).ready(function ($) {
var allUsers = $('.user-row');
$('#user-search').on('input', function () {
var searchValue = $(this).val().toLowerCase();
allUsers.hide();
allUsers.filter(function () {
return $(this).text().toLowerCase().indexOf(searchValue) !== -1;
}).show();
});
});
</script>
<?php
}
add_action('wp_footer', 'plugin_custom_script');
paged
is used in the main query, reusing it for your shortcode means that if your shortcode has 5 pages, but the page its on has 2, then page 3 will 404. You should not reuse query variables, that's a bug that cannot be fixed without changing to use something other thanpaged
/query variables. Other than that though, it's not clear what your problem is as it's phrased in a very weird way regarding what you meant by visible rows. Are you trying to say that you're unable to view a 2nd page of users? Or that all pages show the same users regardless of the page you're on?paged
though, you can set a different URL parameter via theformat
option in paginate links. Likewise you could implement search via PHP using a URL parameter and it would avoid this problem