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.
-
what do you mean by new tables?– MiguelCommented Mar 28, 2013 at 15:39
-
Sorry meant to say that I've added a few extra fields onto the users table.– user1888564Commented Mar 28, 2013 at 15:44
-
2adding columns to the users table is most certainly a bad idea, this is what user meta data is for.– MiloCommented Mar 28, 2013 at 15:55
-
3I STRONGLY suggest you use User Meta rather than Columns in the user table. This is incredibly bad practice– Tom J Nowell ♦Commented Mar 28, 2013 at 16:04
-
2you should never modify anything in core, changing the schema could produce unpredictable results and an update could overwrite your changes.– MiloCommented Mar 28, 2013 at 16:35
1 Answer
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:
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.';
}
-
whats different from using users table than using meta table, slightly confused? Commented Mar 28, 2013 at 16:40
-
3Modifying the user table is horrendously bad practice. NEVER modify core tables, use the provided APIs and datastructures and take advantage of the caching and helper functions provided to you. Modifying the user table could hurt performance, make upgrades fail, is nonstandard, and uses raw SQL queries.– Tom J Nowell ♦Commented Mar 28, 2013 at 17:42
-
Also the meta table was built specifically for storing additional user details, many people spent many hundreds of manhours testing refining and debugging, can you really do a better job with a handful of extra columns?– Tom J Nowell ♦Commented Mar 28, 2013 at 17:43
-
hmm encase you misunderstand, you shouldn't be adding columns to the user meta table either, user meta has key value pairs attached to a user ID– Tom J Nowell ♦Commented Mar 28, 2013 at 18:10
-
ok I now understand...apologies wordpress, I suppose I need help storing 4 string sets and 2 datetime's inside the meta table? Commented Mar 28, 2013 at 19:47