Add additional user fields and make them sortable in the user screen

Additional user info

A quick and easy way to add custom fields to your users. Also a bit of MySQL (just a bit) :D

Last week I wasn’t around so I had no time to write a tutorial, but this week we’re back on track :D

In this tutorial I’ll show you how to add custom fields (select, input, you can place even radio buttons in) in your user screen. Say you want to add more information about your users (especially handy when you have a user base on your site and you want to add some additional information about them besides the preexisting ones in WordPress). In our example here, we’ll put in gender, age and some custom category as fields. In your functions.php  file, or other file that you’ve included in your theme, add the extra fields like this

This will add our select and input fields at the bottom of the users screen. We used two hooks here: show_user_profile  and edit_user_profile. Both are standard hooks to use when adding additional fields to your users. Learn more about them here.

Next we need to save our new data

Noticed that we are escaping our data here. This is important, because we’ll need to use this data to run custom queries for sorting, and you don’t want to be able to add a bad code to your database in any way – on save or on custom query. When doing a custom query it’s best to prepare your query, this is the safest method there is, but sometimes you can bypass it if you know that the query won’t be susceptible to outside manipulation.

Single user screen

Single user screen with new fields added

Sorting it all out

Let’s say that we want to show this new cool data we added in the users table, and that we want to sort users by our newly created fields. First we need to add new columns to the existing user columns.

Great, so now to sort them

Usually you’ll see articles that explain how to sort custom columns, whether they are used for custom post types, or regular posts. Like for instance this one. And that’s great. If you have alphabet or numerical sorting. But what if you have something other? Like our gender column? Then things don’t sort as you want them to. Btw the above code is fine to use on custom column sorting in posts or CPT as well.

What we used here is   pre_user_query hook. You can read more about it here. It’s kinda analogous to pre_get_posts hook, but for users. Basically we’re tapping into the sorting query (that already exists) and we’re modifying it a bit with our custom query. For this you’ll need a bit of MySQL knowledge.

What we’re doing is joining the usermeta table with the users table where the user id matches in both tables, and we’re watching our meta keys we’ve added, and then just use the query_orderby to order our meta values for the specified keys.

Bonus code

For the little extra code I’ll add the export to .csv (comma separated values). This comes in handy if you want to create a mailing list or manipulate your users in excel (or excel like programs)

I’ve added the blue button for export in the tablenav top and bottom part of the users screen. The whole thing looks a bit like this

Users Screen

Users screen with all the new info and export button

And this is it, simple way to add new info and fiddle with it the way you like. This code is cool, as it can be adjusted for regular sorting, and adapted to your needs :) Hope you find it useful and if you have any question, post them in the comments below. Happy coding!

Also published on Medium.

Join the Discussion