Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Integrate Yoast and CoAuthors Plus in WordPress

Integrate Yoast and Co-Authors Plus

If your WordPress site utilizes both Co-Authors Plus and Yoast, you may be aware that the two plugins don't fully integrate with eachother. The PHP code below fixes the following issues:

  • When viewing a Co-Author's page, Yoast's author name (%%name%%) meta tag template variables will properly show the Co-Author's name.
  • When viewing a Co-Author's page, Yoast's breadcrumb trail will properly show the Co-Author's name.

How to install

Simply drop the code below into your theme's functions.php file.

<?php
function replace_author_name_with_coauthor_name_in_yoast_title($replacements) {
if (function_exists('get_coauthors') && is_author() && in_array('%%name%%', array_keys($replacements))) {
$author = get_queried_object();
$author_name = $author->display_name;
$replacements['%%name%%'] = $author_name;
}
return $replacements;
}
add_filter('wpseo_replacements', 'replace_author_name_with_coauthor_name_in_yoast_title', 10, 1);
function output_coauthor_name_in_yoast_breadcrumbs($output) {
if (function_exists('get_coauthors') && is_author()) {
$options = WPSEO_Options::get_options( array( 'wpseo_titles', 'wpseo_internallinks', 'wpseo_xml' ) );
$regex_str = '/(class="breadcrumb_last">' . preg_quote($options['breadcrumbs-archiveprefix']) . ')(.*?)(<\/)/';
$author = get_queried_object();
$author_name = $author->display_name;
$output = preg_replace($regex_str, '$1'. ' ' . $author_name .'$3', $output);
}
return $output;
}
add_filter('wpseo_breadcrumb_output', 'output_coauthor_name_in_yoast_breadcrumbs', 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.