How to make a WordPress profile page

If you're running a multi-author blog, you've probably thought about including biographical and contact details for each author either on the posts that they write, or on a separate profile page. Though WP admin's users section gives you plenty of options for inputting information, from a potted life-story to your AIM handle, most themes don't take advantage of this. So today, we're going to look at setting up profile pages for WP members. If you just want to copy the code and get on with it, scroll down a bit - but I think it's useful to look at how WordPress works first ;-)

author.php and template hierarchy

WordPress can automatically generate a page for each registered user of your blog: this will work irrespective of user level (from subscriber to administrator) and irrespective of whether the user has actually published any posts or not.

The template file used to do this is author.php. However, most themes do not have this file. The template hierarchy says that if author.php doesn't exist, WP should use archive.php to process the author page: this is what most themes do, and that's why most author pages are just a list of that author's posts (and if that user hasn't published any posts, the page just says that nothing can be found).

So let's add author.php to our theme. I'd like to include the following information:

  • user's nickname, and their real name,
  • their email address
  • biographical information
  • a link to their website
  • a photograph
  • links to their most recent posts
  • links to their most recent comments

First, fill out your information: make sure the sections of the user profile you'll want to use are filled in.

Author information outside the Loop

WordPress provides a number of tags to automatically display author name, URL etc. but unfortunately for us, these only work within the Loop. I'm going to use the Loop to display my user's last ten blog posts: unless I want their name and photo displayed ten times as well, I need to find another way to get this information.

The WP function get_userdata comes to our rescue. This lets us find the user information based on the author ID passed to the template:


<?php
$thisauthor = get_userdata(intval($author));
echo $thisauthor->first_name . " " . $thisauthor->last_name; ?>
?>

A full list of available values for get_userdata can be found in the Codex.

For the photograph, users can either sign up with Gravatar (which is the easiest option), and include the photograph like this: <? if(function_exists('get_avatar')) {
echo get_avatar($thisauthor->user_email, 96, "" );
} ?>

Alternatively, use the Author Image plugin, which will add an option to upload a user photograph to WP admin.

To add links to posts, we can use the usual while (have_posts()) loop; I'm going to make this a list of links to post titles, rather than entire posts.

Custom select query to find user comments

Finally, I want to add a list of comments that the user has left. If you have non-blogging members of your blog (and especially if you force registration in order to comment), I think this is a nice thing to do: it makes the user part of your blog community and shows that you value the content they're contributing.

Unfortunately, there isn't a neat inbuilt function to do this for us in WordPress, so we're going to have to make a custom select query, using the user's ID number to look up the comments they've left.

(Note: only comments left while signed in will be found using ID number; you could also look up by email address if you want to find more comments, though of course that will only work if your users are consistent in the email address they use: mine aren't!)

This post is long enough already, so I'm not going to go into custom select queries here; we'll save them for another day. The code I've used to generate my own profile page is below: if you have specific queries about how it works, leave me a comment. I've tested it in WP2.8 *only* btw.

<?php
$thisauthor = get_userdata(intval($author));
?>
<div style="float: right;">
<? if(function_exists('get_avatar')) { echo get_avatar($thisauthor->user_email, 96, "" ); } ?> </div>

<h2><?php echo $thisauthor->first_name . " " . $thisauthor->last_name; ?> (<?php echo $thisauthor->display_name; ?>)</h2>
<p><a href="<? echo $thisauthor->user_url; ?>"><? echo $thisauthor->user_url; ?></a><br />
<a href="mailto:<? echo $thisauthor->user_email; ?>">email</a>
<p><?php echo $thisauthor->description; ?> </p>

<?php if (have_posts()) : ?>
<h2>Recent Posts </h2>
<ul>
<? while (have_posts()) :  the_post(); ?>
		<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; else: ?>
<li>This user hasn't published any posts. </li>
<?php endif;  ?>
</ul>
<?
$querystr = "
    SELECT comment_ID, comment_post_ID, post_title
    FROM $wpdb->comments, $wpdb->posts
    WHERE user_id = $thisauthor->ID
    AND comment_post_id = ID
    AND comment_approved = 1
    ORDER BY comment_ID DESC
    LIMIT 10
 ";

 $comments_array = $wpdb->get_results($querystr, OBJECT);

if ($comments_array): ?>
<h2>Recent Comments </h2>
<ul>
<? foreach ($comments_array as $comment):
	setup_postdata($comment);
	echo "<li><a href='". get_bloginfo('url') ."?p=".$comment->comment_post_ID."#comment-". $comment->comment_ID ."'>Comment on ". $comment->post_title ."</a></li>";
endforeach; ?>
</ul>
<? endif; ?>

Making this work with your theme

  • Copy your theme's archive.php into Notepad or another plain text editor (NOT Word!)
  • Find the section that begins <?php if (have_posts()) ... and ends <?php endif; ?>
  • Replace it with the code in the box above
  • Save it as author.php and upload via FTP to your theme's folder in wp-content/themes
  • Go to Appearance > Editor and make sure that author.php appears in the list on the right

Linking the profile page

<?php the_author_posts_link(); ?>
will generate a link to author.php for a post's author, with their public display name as the anchor text. You could use this as a byline under post titles, for example.

And just to prove that this will work with any registered user, here it is for a subscriber who's never published a post.

I'm happy to add this to anyone's theme for them if they don'../../../contact/">Drop me a line with a link to your blog and which version of WP you're using.

Share this post:
  • email
  • del.icio.us
  • Facebook
  • FriendFeed
  • LinkedIn
  • StumbleUpon
  • Tumblr
  • Twitter

Tags: ,

Posted by Sue on June 27, 2009 in Themes, WordPress.

29 comments to "How to make a WordPress profile page"

  1. Fraser wrote:

    This may be a silly question, but what happens if your theme doesn't have an archive.php file?

  2. Jabril wrote:

    I'm new to WordPress and .php development but not to internet technology. I am having a problem with understanding how I get a static page with the bio of all the bloggers on my site to show up in my WP themed site. I followed your instructions on this post but still can't see a static page with the information the code is supposedly retrieving. Any help will be greatly appreciated!

    Thanks!

    • Sue (@blogmum) wrote:

      Jabril, a static page with the bio of all the bloggers on my site is not what this code creates.

      This code creates an author page for each individual author. If you add the author link (in the very last section of my post) to your index.php template, you should be able to see if it is working or not.

      If that doesn't help, then please leave a link to your blog and I'll try to help further.

  3. Jabril wrote:

    Thanks Sue, that helps! Any advice on adding a static page with bio's listed (maybe with photo)?

  4. peter wrote:

    Hi Sue,

    Love your clear explanations. Please give us more.

    My question: I created the author.php file. Problem is I don't see the author pages. Are they automatically generated or am I missing something? Is there a standard Wordpress setting I need to check. Or something else? Thanks for your time with this.

    Peter

    • Sue (@blogmum) wrote:

      Hi Peter,
      I think there are a couple of problems (hard to diagnose with absolute certainty without looking into your WP admin, but this is where I would start).

      Firstly, I don’t see the author pages
      Your theme appears to be using the_author() to put the author's name underneath the post titles. If you want a link to the author archive page, you need to use the_author_posts_link()

      But even if you do that, something isn't quite right. e.g. have a look at
      http://www.charlestontoday.net/author/ChasToday/
      Chas Today has written a few posts, so there should be something there - but there isn't. There's also a stray PHP closing tag - so I'd have a check over that code and see what's maybe been cut and pasted incorrectly.

      Let me know if you need any more help.

  5. Indrit (@alstudent) wrote:

    Nice work, well done. Very inspiring I would say. I like to do something similar to this, but I want to associate e profile.php page for users who want to register and who can have some possibilities to do in the website, like make comments, add photos, upload their works, but still this is a good work. I can settle this down for my website.

  6. kanishka wrote:

    hi!
    can you help me out. the default page for writer, I already had with the theme shows much information on the sidebar itself. moreover, when i clicked at the author name, i had an archive page for the author,that showed excerpt and other post details too.
    I liked the things very much, but cant i have the excerpts and other details too along with the other personal information after using your trick.
    help me out please

    • Sue wrote:

      Kanishka, yes, of course you can but you'll have to make extensive edits to the code you have. If you want to hire me to do it for you, please get in touch.

  7. Lynne wrote:

    Hi

    Great blog - and your instructions are so clear. I have my author page working as I want apart from one thing - I would like author names with no posts still to link to the profile page. Have googled for quite some time now but can't find a good clear solution. Is this possible?

    All I can think of is writing a blog post for each member to trigger the link but there are quite a few so would rather avoid this.

    • Sue wrote:

      Lynne, I'm not entirely clear what you mean by "author names with no posts linking..." - where do you want them to link from?
      That said, author pages still exist for registered users with no posts: e.g. here's one I just made on TameBay:
      http://tamebay.com/author/test
      It happens automatically within WP when you create the new user.

      • Lynne wrote:

        Thanks for your response! Yes I didn't explain it very well - I now realise that I don't mean to refer to the author.php file - it's the page before that which is a simple list of authors.

        Currently a post has to be written by the author before the links in the list of authors become active and link through to the author page. I was wondering if there was any way I could make the author name in the list into a link prior to a post being written. It's my (probably kludgy) way of creating a member directory where not all members can be relied upon to write posts.

        Hope this explanation is clearer (although I fear it isn't!)

  8. Reeni wrote:

    Hi Sue,
    Just wondering if you have a plugin or know of one where each one of my registered users, when they register, will make an automatic post for them. I am using wordpress for a class and I need each student to have all there bio info on a post and I need it archived under their name. I used to do this manually for all my students when I had 30 in a class. Now I have 150 student and use a search engine for students to find comments with their name in it so other students can find them and make comments on their comments. Will this code you mentioned here help with that. A plugin would sure be nice.

    Thanks for any help. Reeni

  9. Chris wrote:

    Hey Sue,

    I just wanted to let you know how awesome your posts are and how handy they have been. I've implemented this coding easily and love it. Thanks so much for all the information you are writing and sharing!

    -Chris

  10. Seb wrote:

    Very nice work!
    But you forgot some "<?php" in your code, didn't you?

  11. Hamed wrote:

    Thanks for the information I know this post is a bit old but I was able to build a profile page from your instructions. Now I would like to also add the avatar to posts as well however I can't find a function that will work inside the post loop.

  12. Ian Henderson wrote:

    i host 5 of my blogs on Blogspot and it is really good for beginners. but if you want something with more features, nothing beats wordpress;,`

  13. Great information you write it very clean. I’m very lucky to get this details from you.

  14. Squidz wrote:

    Hey Sue - was checking your blog as I'm looking to do something similar to what you outline here. However, neither of your links to the final product work...not Sue and not Fakeuser...not "test" on Tamebay. All return a 404.

    I'm curious how these look. Your tutorial seems to cover nearly everything I'm looking to do sans attaching downloadable files.

  15. extenze wrote:

    The data is very comprehensive. It tackles a broad spectrum of ideas. Thank you for giving me a chance to see things the way you do.

  16. Gail Sayavong wrote:

    I ask all my good friends to check out this weblog.... and I hope they will surely like..

  17. Thanks for taking this possiblity to discuss this, I'm strongly regarding it and I make use of learning about this subject. If at all, while you gain data, please update this site with new information. I've found it extremely useful.

  18. isti wrote:

    i know profile page is important. but i havenot given much thought or done it anyway... maybe i need to do this now.

  19. Towfiq I. wrote:

    Thanks!! Love your code...
    Is there anyway to show the registration date???

    I tried this:

    user_registered('F j, Y');?>

    It throws this error:
    Fatal error: Call to undefined method stdClass::user_registered()

Leave a Reply