Skip to toolbar

Step 6: Additional Options

Before finishing we will learn how to restrict access to the user directory and limit which users will be in the directory. We will also cover other methods for getting the values of fields that are better suited for when we only need one or two of the fields at a time.

Only Show Certain Users In User’s List

You may only want to show users with a certain role in your directory if, for example, your site allows for subscribers, but you want a directory of contributors. If this is the case, you just need to specify which role in the get_users function we are using to build our list. Another option you can employ is to add a boolean field to the user profile called ‘Hide Directory’. Here is an example of how to only show users with the role of contributor or higher that we have chosen not to hide from the directory:

Other fun options you can try include exclude specific users with the ‘exclude’ argument or filter the list with WP_Query.

Restrict Access

In my example we are listing some fairly personal information that you may not want to be made public to just anyone on the internet. You can restrict access to logged in users, with specific roles, by wrapping the profile in a check like this:

Getting One Field At A Time

In previous examples we were getting all of the meta fields for a user at once, which made sense since were using almost all of them. This is overkill if you only need the value of one field. If we need to get a meta value for a post’s author we can use get_the_author_meta to show a field’s value. Inside the loop we don’t even have to specify an ID. When we are listing the value for a field for several users it is better to use get_user_meta, which is a very similar function. Here are examples of both functions in action:

The first function uses get_the_author_meta to get the author’s twitter profile url and uses it to create a Twitter follow button. This function must be used in the loop and only works for users who are post authors. The second example can be used outside of the loop and for any registered user. It displays a link to their Twitter profile. This function can be easily adapted to show any type of field and can be used inside of other functions as needed.

Continue to Conclusion.