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!
Just what the doctor ordered. Thank you!
Thanks! This article pointed me in the right direction….
hi this article helped me immensely!1 Thank you!! I am just having a bit of an issue pulling in the avatar from the guest author. On the co authors > Guest Author page there is a meta featured image which Im assuming this is whats supposed to be pulled in…however it seems to only pull in the default wp gravatar. Any advice?
Ah ha! Nevermind! With all the back and forth with trying to get this to work. Between adding a WP user and a guest author, I forgot to add one! Whoops my bad. Anyway, great article. You’ve showed me more than the Co-Authors website and support forum. :(
:)
Glad to hear your figured it out & that this helped you Lance :)
I have one question Im hoping you might help with. Sorry!! I’ve got this scenario where I have several companies writing articles on my site. Company A, Company B and Company C. Sometimes there isn’t an actual writer for those companies.
For example, Company A writes an article. I have that company as the author because there is no specific writer for that company or no coauthor. First, I set up the companies to be users. Then I have some actual people writing on behalf of those companies. I set those people to be guest authors. I wanted to know if there is a way to distinguish if which author is being used? So, if there is a co-author being used on the post – show their gravatar, title and description else just show the normal author.
Im using underscores me from automattic and they have a specific function that just shows posted on date and by Author. I’d like to show that when there is no co-author. I see up top you have an if isset coauthor type, show coauthor else show the normal user. I’m having a bit of a time getting that to work. I’ve also tried to set the if function exists as well.
I should note that on posts where there is a coauthor, I took the main author off because I don’t want to show the standard two authors. Do you have any advice? Thank you!! Sorry if that was confusing!
After checking different scenarios of taking the main author off, using only the coauthor or both it seems the if isset condition is failing. It seems to just by pass the first statement where you output the coauthor code and goes directly to the else part of the condition. So what exactly is that $coauthor->type. I’ve looked in the files but cant really decipher what that type is. UGH I swear designers that know nothing of WP are going to be the death of me!!! lol!!!
Hey Lance, co-author type is whether or not they’re a registered user, so
'guest-author' === $coauthor->type
would check for the co-authors that don’t have an account on your site, while'guest-author' !== $coauthor->type
would give you only registered authors (registered WP Users) on the site.Thanks! Beka! I managed to get it working :)
Thanks, this is helpful.
Guest Authors have their own avatars as well (and a number of additional info fields) so these need to be checked for and fetched separately too, right?
Here’s a more readable syntax for displaying co-author avatars:
Thanks for the addition Dan! That’s what I was getting at in the examples section, the printf syntax may make it a bit easier for others to see here though :)
Here’s the same code (and more) as a gist for use with Theme Foundry’s Make theme — https://gist.github.com/newlocalmedia/9eab98c0f3d3e962dcf03b581e5d3263
i can’t extract Contact Info Type like Yahoo Aim or other info on contact info section
i need to show it on Author box, but i didn’t find any template tags to get it from php
Hey,
I am using this plugin and having a problem while displaying avatar. I want a default avatar to be shown if more than 1 author have contributed to a post, else, just show the user’s avatar. Do you know how it can be done?
Thanks for your inputs. They have been really helpful!
Is there an easy way to list all guest authors with their biography on a page with a link to their posts? Basically, I need to have a people page which lists all guest authors with their name, photo, bio and link to their posts.
Any help would be greatly appreciated!
Check out the
coauthors_wp_list_authors()
method, sounds like a good fit!Thanks for that great tutorial! That damn co-author avatar works just fine now :)
Hey Beka and thanks for the great article. I’ve been using this plugin for quite a while now, but recently I’ve discovered that if I change a user’s email, they are unassigned from all their posts. I guess this is a bug in the plugin, but I couldn’t reach the plugin’s author/support/github, so I’m hoping you could maybe point me to the right direction.
I either need a fix for this bug, or just a simple function I could run to reassign users to the posts.
Thanks a lot,
Itmar
Thank you! Made my day!
Hey Beka, been a while! ? Just wanted to let you know that this is still my go-to post for dealing with Coauthors Plus :) Here’s a virtual beer for you all ?