In part three of our series recapping my presentation on Enhancing WordPress User Profiles, we’ll go through the steps and code involved in displaying the new fields we added to the database using functions.php.
For our demonstration, I am creating a team biography page for a corporate or small business website. Using WordPress’ User Profiles, we can create a user account for each team member, and grant them access to maintain their own information. I’ve also created a special role in WordPress for my team called “Team” using Capability Manager. This allows me to grant specific permissions to these users and keep them organized.
First, we define our globals and variables, and start our loop through all the users that are assigned to the ‘team’ role.
get_col("SELECT ID FROM $wpdb->users ORDER BY 'ID' ASC"); // query users foreach($user_ids as $user_id) : // start users profile "loop" $user = get_userdata($user_id); // retrieve user details $level = $table_prefix . 'user_level'; // set 'user_level' usermeta meta_key record $user->user_level = $user->$level; // assign 'user_level' property to $users $role = $table_prefix . 'capabilities'; // set 'role' usermeta table meta_key record $user->role = array_keys($user->$role); // assign 'role' property to $user $user->role = $user->role; // make sure $user->role is not an array if( $user->role == 'team' ) : // if the user has the role of 'team', proceed $image_dir = 'wp-content/uploads/userphoto/'; // directory where author images reside $image_file = $user->userphoto_thumb_file; // format for image name $image_path = trim($image_dir, '/') . '/' . $image_file; $author_image = get_bloginfo('home') . '/' . $image_path; ?>
Then we set up the HTML and calls to the database. This is the code that repeats over and over as we loop through all the users on the team.
Lastly, we close up our check to see if the users are part of the ‘team’ and our loop.
Now, we have a block for each team member that contains their photo, biography, and, if entered, links to their Twitter, Facebook, and LinkedIn accounts.