admin管理员组

文章数量:1134243

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' => '&laquo; Previous',
        'next_text' => 'Next &raquo;',
    ));
    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');

本文标签: pluginsSearch results not finding in paginated pages just in visible rows