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:

        $args = array(
            'orderby'       => 'user_lastname',
            'role'          => 'contributor'
            'meta_key'      => 'hide_from_directory',
            'meta_value'    => '',
    	$users = get_users($args);
		foreach ($users as $user) {
			echo '<li><a href="' . esc_url( get_author_posts_url($user->ID) ) . '">' . esc_attr($user->user_lastname) . ', ' . esc_attr($user->user_firstname) . '</a></li>';

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:

$current_user_id = get_current_user_id();
$current_user = new WP_User ( $current_user_id );

    if ($current_user->roles[0] == 'contributor' || 'administrator') {
		//show profile
    elseif ( ! is_user_logged_in() ) {
        echo '<p><strong>You must be logged in to view the user directory.</strong></p>'
		else {
        echo '<p><strong>Sorry, but you do not have sufficient privileges to view the user directory.</p></strong>'

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:

//Output a twitter follow button for post author. Must be used in loop
//See for more info
function pods_twitter_follow() {
	$twitter = get_the_author_meta('twitter');
	$name = get_author_meta('user_firstname');
	<a class="twitter-follow-button" data-show-count="false" href="<?php echo $twitter; ?>">Follow <?php echo $name; ?></a>
	<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);;js.src="//";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
//Output a link to a user's twitter page
function pods_show_twitter_link($id) {
	$twitter = get_user_meta($id, 'twitter', true);
	echo esc_html($twitter);

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.