Documentation Incorporate Co-Authors Plus template tags into your theme

Incorporate Co-Authors Plus template tags into your theme

Overview #

Co-Authors Plus is a plugin maintained by WordPress.com VIP which makes it easy to assign one or more bylines to a post. Upon activation, you’ll be able to start assigning multiple bylines right away. In order for those bylines to appear on the frontend, you may need to make some small modifications to your theme.

↑ Top ↑

Available Template Tags #

WordPress offers template tags like the_author() and the_author_posts_link() to display byline information associated with each post post. You might see these inside files like single.php and author.php

Co-Authors Plus has similar template tags for displaying multiple bylines. These are located in the template-tags.php file. The most relevant are:

/**
 * Outputs the co-authors display names, without links to their posts.
 * Co-Authors Plus equivalent of the_author() template tag.
 *
 * @param string $between Delimiter that should appear between the co-authors
 * @param string $betweenLast Delimiter that should appear between the last two co-authors
 * @param string $before What should appear before the presentation of co-authors
 * @param string $after What should appear after the presentation of co-authors
 * @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
 */
function coauthors( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ){
	return coauthors__echo('display_name', 'field', array(
		'between' => $between,
		'betweenLast' => $betweenLast,
		'before' => $before,
		'after' => $after
	), null, $echo );
}

/**
 * Outputs the co-authors display names, with links to their posts.
 * Co-Authors Plus equivalent of the_author_posts_link() template tag.
 *
 * @param string $between Delimiter that should appear between the co-authors
 * @param string $betweenLast Delimiter that should appear between the last two co-authors
 * @param string $before What should appear before the presentation of co-authors
 * @param string $after What should appear after the presentation of co-authors
 * @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
 */
function coauthors_posts_links( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ){
	return coauthors__echo('coauthors_posts_links_single', 'callback', array(
		'between' => $between,
		'betweenLast' => $betweenLast,
		'before' => $before,
		'after' => $after
	), null, $echo );
}

/**
 * Outputs the co-authors display names, with links to their websites if they've provided them.
 *
 * @param string $between Delimiter that should appear between the co-authors
 * @param string $betweenLast Delimiter that should appear between the last two co-authors
 * @param string $before What should appear before the presentation of co-authors
 * @param string $after What should appear after the presentation of co-authors
 * @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
 */
function coauthors_links($between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
	return coauthors__echo('coauthors_links_single', 'callback', array(
		'between' => $between,
		'betweenLast' => $betweenLast,
		'before' => $before,
		'after' => $after
	), null, $echo );
}

Each of these template tags will present different aspects of the multiple bylines. For instance, the first will display the first and last name of each co-author without any links. The second will display the first and last name of co-author linking back to their author profile page. And so on.

↑ Top ↑

Integrating Template Tags Into Your Theme #

To integrate Co-Authors Plus, you’ll want to replace existing author template tags in your theme with a simple conditional that uses the Co-Authors Plus template tags if Co-Authors Plus is available. The conditional prevents your site from breaking (e.g. white screen of death) if Co-Authors Plus isn’t activated.

For example, here’s how you would update the_author_posts_link() to instead use coauthors_posts_links():

if ( function_exists( 'coauthors_posts_links' ) ) {
    coauthors_posts_links();
} else {
    the_author_posts_link();
}

However, the example above is a relatively simplistic way of presenting bylines. There’s a good chance your theme will need an adaptation of it.

For instance, here’s how the change looks for the Hybrid theme:

function hybrid_entry_author_shortcode( $attr ) {
	$attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
	if ( function_exists( 'coauthors_posts_links' ) ) {
		$author = coauthors_posts_links( null, null, null, null, false );
	} else {
		$author = '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '" title="' . esc_attr( get_the_author_meta( 'display_name' ) ) . '">' . get_the_author_meta( 'display_name' ) . '</a></span>';
	}
	return $attr['before'] . $author . $attr['after'];
}

Here’s the new function for TwentyTen:

if ( ! function_exists( 'twentyten_posted_on' ) ) :
/**
 * Integrate Co-Authors Plus with TwentyTen by replacing twentyten_posted_on() with this function
 */
function twentyten_posted_on() {
	if ( function_exists( 'coauthors_posts_links' ) ) :
		printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
			'meta-prep meta-prep-author',
			sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
				get_permalink(),
				esc_attr( get_the_time() ),
				get_the_date()
			),
			coauthors_posts_links( null, null, null, null, false )
		);
	else:
		printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ),
			'meta-prep meta-prep-author',
			sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
				get_permalink(),
				esc_attr( get_the_time() ),
				get_the_date()
			),
			sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
				get_author_posts_url( get_the_author_meta( 'ID' ) ),
				esc_attr( sprintf( __( 'View all posts by %s', 'twentyten' ), get_the_author() ) ),
				get_the_author()
			)
		);
	endif;
}
endif;

Here are a couple more threads you can look at for insights: