How to Add Author Information Blocks To Your Site

There are several reasons why you’d want to show author information blocks on your site. It could be either because you have multiple authors, or you allow for guest posts on your blog. One of the reasons why people would want to guest post is because they can gain some extra exposure.

A cool way of doing this is using the built-in author functions with some extra’s to add a “Guest post by” block to your template. I’ve done this on yoast.com, and you can find an example here.

How I did this? Using Gravatars and the new author_meta functrions in WordPress of course!

WordPress has added template tags you can use to show author info and Gravatars, so it’s pretty easy. So you can just add the following code to your theme. I’ve annotated all lines, you should be able to copy paste and modify:

<?php
  // If it's not written by me (username admin), it's a guest post,
  // hence check for that:
  if (get_the_author_meta('user_login') != "admin") {
        // If it's a guest post, show the appropriate block
?>
    <div class="guestauthor" id="
      <?php the_author_meta('user_email'); ?>">
<?php
        // Make sure the image is right aligned
        function avatar_align($avatar) {
            return str_replace("class='","class='alignright ",$avatar);
        }
        add_filter('get_avatar','avatar_align',10,1);
  
        echo get_avatar( get_the_author_meta('ID'), '60' );
        // Show the author's information
?>
        <h3>Guest post by: <?php the_author_meta('display_name'); ?></h3>
        <p><?php the_author_meta('description'); ?></p>
    </div>
<?php
    }
?>

The last thing is that, in order for HTML to be allowed in user profiles, you need to add the following line to your themes functions.php:

// Allow HTML in user profiles
remove_filter('pre_user_description', 'wp_filter_kses');

That’s it! You now have cool “Guest post by” blocks on your site!

About Brian Gardner
I am a hopeless Starbucks addict, freelance web designer/internet consultant living in Chicago. I write poetry and also love to ski. Lastly, I am the CEO/founder of StudioPress, which develops WordPress themes for business and individuals. Follow me @bgardner.

Comments

  1. reb says:

    Thanks, Joost! I can think of several sites this functionality will be perfect for :)

  2. Benny says:

    Does this require wp 2.8? Will it work on WPMU 2.7?

  3. This is great. I do lots of sites for churches and this would be great to add for speakers to go with podcast episodes. Thanks.

  4. Joi says:

    JUST what I was needing – thanks!

  5. john says:

    is there an example of this code in use?

  6. Stephanie says:

    Joost –

    I see the second block of code goes in the funtions file. Where do you put this first block of code? In the header.php file?

  7. Jocelyn says:

    Hi Joost,

    Could you please answer Stephanie’s question about where to place the first block of code. It is a excellent question that you didn’t answer in the post. I would like to use your tip and need to know where to place it.

    Thanks so much in advance, Jocelyn

  8. Marty says:

    Can this code be set up so non-guest would include other users as well as admin?
    For example, a site is set up so “authors” would be “guest bloggers”, and all other users
    except subscribers would be staff bloggers for the site?

  9. Remo Knops says:

    I think it’s not only cool for guest-posters, but also for the core team of a website. I assume the user information is loaded from the WordPress Profile (more in particular from the “Biographical Info” text).

  10. Mike says:

    Where does the top code go? Since Joost isnt answering, does anyone else know?

  11. Liseth says:

    Thanks for this cool trick but as a very new user I am just learning how to style my themes.

    I found the index.php on my lifestyle theme, but between what and what should I paste this? I don’t want to mess up the theme!

    Thanks

  12. Halim KILIC says:

    this not working for me :(

Speak Your Mind

*