Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. Creative Coding

Show Yourself Off With a Custom Author Box

Read Time:12 minsLanguages:

Multi-Author blogs are becoming increasingly popular and with good reason. Creating regular, engaging content can often be a challenge for an individual blogger. With multiple authors it can be a lot easier and also allows you to cover a greater range of topics pulling from the knowledge of several people. On sites like Wptuts+ you get to read articles from a massive team of writers and bloggers, we all have our own writing style and personalities. Like Wptuts+, on most multi-author sites, you will find a nice little author information box somewhere on the page. Today I'm going to show you how you can create one for your very own site.

WordPress is a ready made multi-author blogging and content platform. All the tools we need are built in we just need to know how to use them.

It All Starts With a Profile

Hidden away on the WordPress admin bar (when logged in) all users have access to the "Edit My Profile" link. It takes you to the page where you set how your name is displayed on the site, it's where you change your password and email address. It's also the place where you can enter really useful, modern and current contact information such as AIM, Yahoo IM and Google Talk details (/sarcasm). I know, because I do the same, you've always ignored most of it. If you don't use these fields then why fill them in? Our author box is going to use these fields and we are going to make them a little bit more useful!

WordPress Profile PageWordPress Profile PageWordPress Profile Page

Extra Fields

It is very easy for us to remove the contact methods we don't want to use and replace them with something a little more current. So first things first you need to open up your theme's functions.php (or stick this in a plugin if your prefer) and let's get to work.

So first thing we are doing is removing some fairly useless fields, next we simply add the additional fields we want. The first part needs to be unique for the text you can enter whatever you like as this is what will be displayed next to the field on the profile page.

If you revisit your edit profile page you should now see your additional fields have appeared and the useless ones have disappeared.

Profile with Custom FieldsProfile with Custom FieldsProfile with Custom Fields

Now we have somewhere we can store our additional information we can move on to working with the data and how we can show it in the front end of our site.

Show Yourself Off

So we want to show a picture of our author, a little description / bio and some links to social media sites.

WordPress handles user profile pics using Gravatar. We want to use the WordPress Function get_avatar() this retrieves the Gravatar for the specified user ID or email address.

You can specify the size of the image and also what to do if the user doesn't have a Gravatar. The final parameter allows you to set the Alternate text for the avatar. For now we are going to use the following code:

This pulls in the Gravatar at 70px x 70px and uses the user ID of the author for the current post.

We also want to pull some of the basic user profile information through like the user's display name and description. Let's have a look at a basic example.

The Basics

The above code gives a nice starting point. Open up your single.php (I am using Twenty Twelve for the purposes of this article) and paste in the above code inside the loop but above your comments template. (Line 25 if you are using Twenty Twelve as well.) Now let's go through what we're doing.

We've already talked about the get_avatar() function but further down you will see the get_the_author() function. This simply retrieves the post author. In our example we print the author's name at the top of our author box. It will display whatever the user has selected as their "Display name publicly as". You could simply use:

However I prefer to escape all my output and allow the option for translators.

Next we see get_the_author_meta(). We're going to be using this quite a bit. This function returns the desired meta data for the given user. If we use it within a loop we don't need to specify the user ID however it can be used outside of the loop by passing the user ID. Using this function you can get a range of information, in the above example we are using it to pull the description which is the biography box you can complete on the user profile page. As with the author name I'm escaping the output to make sure our authors haven't hidden any nasties!

Finally we have a link at the bottom which links through to our author page. We use the get_author_posts_url() function to simply give us the URL. WordPress automatically creates author pages for all users of the site who have published posts.

Let's add some CSS and see how it looks:

Basic Author BoxBasic Author BoxBasic Author Box

Social Shizzle Wizzle

So we've got a basic box, but what about all those extra fields we added? We can use the get_the_author_meta() function to retrieve those custom fields just as we did with the description. Let's go through the following example together, a replacement for our previous "profile-links" div:

We've created an unordered list and each list item will be our extra fields we added at the start of the article. Firstly we do a crude check to make sure that there is a value entered for each of them as we don't want links appearing that don't do anything. Next we create a link based on the information entered in the user profile. As you can see from the example we form the link by using the same get_the_author_meta() function, however depending on what we are outputting we use a different validation function. For the Twitter link as we are only entering a username and appending that to our link we strip any HTML from it using wp_kses() for the others as we are entering full profile URLs into our profile page we are using esc_url() to ensure they are properly formed URLs. I've shown you two different ways of doing it so you can see how flexible it can be.

By adding a little more CSS we can style it and we're almost done!

The CSS I am using for demonstration purposes only you can of course do what you like with your own styling!

Author Box with LinksAuthor Box with LinksAuthor Box with Links

Getting Fancy

Now we have our basic box we can add all kinds of extra features. We're going to create a dynamic LinkedIn Profile Card when you hover over the LinkedIn icon.

First up we need to create some functions. We need to enqueue the LinkedIn JavaScript library on our blog posts and our author pages (more on this later!).

Add the above code to your theme functions.php or wherever your added the code to change your contact methods. This will ensure we now have the required JavaScript loaded but only on pages where it is needed.

Next up we are going to create a little function to perform the hover functionality based on the author we are looking at:

First thing you should notice is that we actually need to pass a parameter to this function when we call it. We want to pass the user ID of the author we want to create the hover card for. Essentially what we are doing is getting the user meta data using get_user_meta() we then pull out our LinkedIn URL that we created in the first half of the article. Then simply add that variable into our LinkedIn JavaScript so that it returns the correct user hover card. Here's the updated single.php extract:

As you can see we call our LinkedIn function by using wptuts_linkedin( get_the_author_meta( 'ID' ) ) this passes through the user ID of the correct author to be used in our function. A couple of simple CSS tweaks to make it show properly:

As you can see now we have our author box with a nice hover card for our LinkedIn profile!

LinkedIn Hover CardLinkedIn Hover CardLinkedIn Hover Card

Author Page

It's often nice to show this author box at the top of an author's archive page. Twenty Twelve already does some of the work for us in author.php. So we can add what we've learnt above to add our extra information.

WordPress Author PageWordPress Author PageWordPress Author Page


So there we have it. You now know how to alter your contact methods, how to pull different information from a user profile and display it on their blog posts. There is so much more you can do depending on what you want to achieve. Try adding a link to the user's website, display their latest Tweets, pull a commit history from GitHub; the options are endless. All the final code can be found below.

Vector Social Media Icons by IconDock.com & Double-J Design

URL: http://icondock.com/free/vector-social-media-icons

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.