Adding Co-Authors Plus Support to Your Theme

I recently added Co-Authors Plus support to a couple of our websites at SkyVerge. This plugin proves to be really useful for magazine or multi-author blogs for a couple reasons.

  • First, you can credit multiple authors for a post, which is helpful if you have a team contributing to an article.
  • Second, you can add “guest authors” without providing a login to your site. This lets you credit guest contributions in the same way you credit regular authors, but you don’t have to have more and more user accounts to accept every guest post. Guest contributors will have post archives and appear to be regular authors to readers, but you get the benefit of not having to provide contributor or author accounts for a bunch of people just to accept a guest post.

One issue I ran into at Sell with WP was related to how guest author properties are named differently than some of the WP_User properties (for regular / “full” authors), so I figured I’d document some of the quirks I ran into. This should hopefully guide you (or a future version of myself ?) through adding Co-Authors Plus support to your theme.

Co-Authors Functions & WP Equivalents

First, there are some functions that are helpful for getting our outputting co-author information in your theme. As you’ll have more than one author for the post, you can’t use the default WordPress functions. Instead, you need to loop through all possible authors and output their information.

The get_coauthors( $post_id ) function is a helpful one to know. It lets you get co-author objects for a post. The function accepts a post ID, but defaults to the current post, so passing the post ID isn’t necessary in the loop.

You may also run into a need for the coauthors_wp_list_authors( $args ) function. This lists all authors on your blog, including regular and guest authors. The $args parameter lets you pass in some arguments, such as whether or not to include authors who haven’t yet authored a post. I’d have a look at this doc block for further details.

There are some co author functions that directly relate to some theme functions with which you’re probably already familiar.

Co-Authors Function WP equivalent Does what?
coauthors_get_avatar( $coauthor, $size ) get_avatar() outputs the avatar for the co-author
coauthors() the_author() outputs author display names in a comma-separated list
coauthors_posts_links() the_author_posts_link() outputs display names linked to author archives in a comma-separated list
coauthors_links() - similar to coauthors_posts_links(), but links to author websites instead of archives

Working with Guest Authors

If you’ll be using guest authors, it’s also helpful to know how guest authors are treated slightly differently than regular authors / WP users. You can first check if an author / co-author is a guest by checking the coauthor type:

if ( isset( $coauthor->type ) && 'guest-author' === $coauthor->type ) {
    // we have a guest author
}

Most of the time, you can output guest author information the same way you’d output regular user or author information. However, there are a couple guest author properties that differ from WP_User properties, so this check will come in handy if you find you want to output them.

WP_User property or meta Guest Author equivalent
$user->ID $coauthor->ID
$user->first_name $coauthor->first_name
$user->last_name $coauthor->last_name
$user->user_login $coauthor->user_login
$user->user_email $coauthor->user_email
$user->display_name* $coauthor->display_name
$user->user_url $coauthor->website
$user->user_description $coauthor->description

*Display name is usually output using the get_the_author() function instead.

Practical Examples

So what will it look like in action? Here are a couple examples:

Example 1: Output the author avatar
We’ll output the author’s avatar, linking the image to the author’s post archive, for each co-author. I’ll break this up a bit solely for readability.

if ( function_exists( 'get_coauthors' ) ) {

  $coauthors = get_coauthors();
  foreach ( $coauthors as $coauthor ) {
    $archive_link = get_author_posts_url( $coauthor->ID, $coauthor->user_nicename );
    $link_title = 'Posts by ' . $coauthor->display_name;
    ?>
    <a href="<?php esc_url( $archive_link ); ?>" class="author-link" title="<?php echo esc_attr( $link_title ); ?>"><?php echo coauthors_get_avatar( $coauthor, 65 ); ?></a>
    <?php
  }
// treat author output normally
} else {
  $archive_link = get_author_posts_url( get_the_author_meta( 'ID' ) );
  $link_title = 'Posts by ' . the_author();

  ?>
    <a class="author-link" href="<?php echo esc_url( $archive_link ); ?>" title="<?php echo esc_attr( $link_title ); ?>"><?php echo get_avatar( get_the_author_meta( 'user_email' ), 65 ); ?></a>
  <?php
}

Example 2: Output author bio
This is where guest author changes come into play, as the guest author bio is accessed differently than a user’s bio. First (assuming we’re already looping through the co-authors), we can check if we have a guest author, and if the bio is set, output it with $coauthor->description. If we have a regular author, then we can check for $coauthor->user_description and output that instead.

<?php if ( isset( $coauthor->type ) && 'guest-author' == $coauthor->type ) { ?>
    <?php if ( $coauthor->description ) : ?>
      <div class="author-description">
        <p><?php echo wp_kses_post( $coauthor->description ); ?></p>
      </div>
    <?php endif; ?>
<?php } else if ( $coauthor->user_description ) { ?>
    <div class="author-description">
        <p><?php echo wp_kses_post( $coauthor->user_description ); ?></p>
    </div>
<?php } ?>

Example 3: Output author website link
This puts us in the same scenario as the example above; while looping through co-authors, you can access the author’s website link usually by using $author->user_url. However, a guest author website URL is accessed by $author->website, so we’d need to check for a guest author first again while looping through them.

<?php if ( isset( $coauthor->type ) && 'guest-author' == $coauthor->type ) { ?>
    <?php if ( $coauthor->website ) : ?>
    <?php printf( '<a href="%1$s">%2$s</a>', esc_url( $coauthor->website ), esc_html__( 'Website', 'textdomain' ) ); ?>
    <?php endif; ?>
<?php } else if ( $coauthor->user_url ) { ?>
    <?php printf( '<a href="%1$s">%2$s</a>', esc_url( $coauthor->user_url ), esc_html__( 'Website', 'textdomain' ) ); ?>
<?php } ?>

You can download Co-Authors Plus from wordpress.org to take it for a spin, but it won’t show up on the frontend of your site without these modifications, so be sure you’re familiar with theming to get it up and running!