get user list in admin area

how can I fetch a list of registered users and show it in the admin panel. I know theres the list inside the admin panel already but I’ve added a few tables and I need to show them in a new tab menu inside the admin panel.

1 Answer
1

What you’ve done is incredibly bad practice, and should be undone immediatley. There is already a dedicated table for storing user details aka User Meta. Altering the user table could cause issues with other plugins and Core, prevent upgrades working, and bypasses all the caching and object stores WordPress puts in place. It also adds additional avenues for SQL injections.

TLDR: Never modify Core WP Tables

Instead use the provided User Meta API:

  • get_user_meta
  • update_user_meta
  • delete_user_meta

They work in the same way get_post_meta etc work, and there is already a dedicated table, and additional APIs for querying via WP_User_Query.

Then display this using the answer here:

How to display multiple custom columns in the wp-admin users.php?

To search for users based on meta use the WP_User_Query class. Using this you can grab lists of users with specified meta data, e.g.

$args = array( // get all users where
    'meta_key' => 'specialkey', // the key 'specialkey'
    'meta_compare' => '=', // has a value that is equal to 
    'meta_value' => 'helloworld' // hello world
);

// The Query
$user_query = new WP_User_Query( $args );

// User Loop
if ( !empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<p>' . $user->display_name . '</p>';
    }
} else {
    echo 'No users found.';
}

Leave a Comment