Step 5: Creating A List Of Users and Profile Page

Now it is time to create a user directory page on our page. This can be as simple as a list of names with links to author profile pages or a full fledged directory with all of the user’s information.

For our last step, we will create a custom page template to show a list of all users with links to their user profile pages. The simplest way to do this is with get_users. Create a new page template as a copy of page.php, and in place of the loop we will use WP_User_Query  to create a list of users, with their names as links to their user pages. Don’t forget to create a new page in WordPress using your new page template.

<ul>
	<?php
		$users = new WP_User_Query(array(
			'meta_key'=>'last_name',
			'orderby'=>'meta_value',
			'fields'=>'all_with_meta'
		));
		if ( ! empty( $users->results ) ) {
			foreach ( $users->results as $user ) {
				echo '<li><a href="' . esc_url( get_author_posts_url($user->ID) ) . '">' . esc_attr( $user->last_name ) . ', ' . esc_attr( $user->first_name ) . '</a></li>';
			}
		}
		else {
			echo 'No users found.';
		}
	?>
</ul>

This simple function gets the users first and last names as well as the links to their profile pages and creates a list of links, ordered by last name. Click here for the complete user’s list template.

Here is where we are going to run into the problem I mentioned above about users without published posts. This may not be an issue for those of you who are using this to show contributors to your site. If you want to show profiles for users that are not authors of posts you will need to add a function to your theme’s function.php to show it. The content of this function is the same as the one we used in our author.php. In fact, you probably want to replace all of the identical code in author.php with a call to this function. Its one argument $user is intentionally identical to the variable $user we built in that step. Here is the complete function:

<?php
function pods_user_profile_display($user) {
	//Escape all of the meta data we need into variables
	$name = esc_attr($user->user_firstname) . '&nbsp;' . esc_attr($user->user_lastname);
	$title = esc_attr($user->title);
	$email = esc_attr($user->user_email);
	$phone = esc_attr($user->phone_number);
	$street_1 = esc_attr($user->street_address_line_1);
	$street_2 = esc_attr($user->street_address_line_2);
	$city = esc_attr($user->city);
	$state = esc_attr($user->state);
	$zip = esc_attr($user->zip_code);
	$website = esc_url($user->user_url);
	$twitter = esc_url($user->twitter);
	$linkedin = esc_url($user->linkedin);
	?>
	<div class="author-info">
		<h2><?php echo $name; ?></h1>
			<div class="author-avatar">
				<?php echo '<a href="' . pods_image_url( $user->picture, 'large') . '">' . pods_image ( $user->picture, 'thumbnail') . '</a>';
				?>
			</div><!-- .author-avatar -->
			<div class="author-description">
				<p><strong><?php _e('Email:', 'twentytwelve'); ?></strong> <?php echo '<a href="mailto:' . $email . '">' . $email . '</a>'; ?></p>
				<p><strong><?php _e('Phone:', 'twentytwelve'); ?></strong> <?php echo $phone; ?></p>
				<div><p><strong><?php _e('Address:', 'twentytwelve'); ?> </strong></p>
					<?php echo
						'<p>' . $street_1 . '</p>' .
						'<p>' . $street_2 . '</p>' .
						'<p>' . $city . ', ' . $state . ' ' . $zip . '</p>';
					?></div>
				<p><strong><?php _e('Website:', 'twentytwelve'); ?></strong> <?php echo '<a href="' . $website . '">' . $website . '</a>'; ?></p>
				<p><strong><?php _e('Twitter:', 'twentytwelve'); ?></strong> <?php echo '<a href="' . $twitter . '">' . $twitter . '</a>'; ?></p>
				<p><strong><?php _e('LinkedIn:', 'twentytwelve'); ?> </strong> <?php echo '<a href="' . $linkedin . '">' . $linkedin . '</a>'; ?></p>
			</div><!-- .author-description	-->
	</div><!-- .author-info -->
<?php
}
?>

Once you’ve added that function you can use it in your users list template, like so:

<ul>
    <?php
		$users = new WP_User_Query(array(
			'meta_key'=>'last_name',
			'orderby'=>'meta_value',
			'fields'=>'all_with_meta'
		));
		foreach ($users as $user) {
			pods_user_profile_display($user);
		}
	?>
</ul>

Here is our new users list template and the updated author.php.

Continue to Step 6: Additional Options.