Add Google Authorship Data to Your WordPress Site, for all Individual Authors

You’ve probably seen people’s profile pictures showing up on Google search results lately. This is all based around linking the content that you create to your Google+ account. Google is likely using this information to rank search results, to determine who is more or less worthy of rising to the top, and probably to help you find content created by people you follow on Google+, too.

I won’t get into the why of doing this, but if you already know that you want to comply with this new technique, here’s some code that will help you achieve that goal.

Note that this will go out and grab data from your User Profile in WordPress (including creating a new field there) and so even if you have multiple authors, they can all get the proper credit on their respective pages of your site.

This goes in your functions.php. Special thanks to Justin Tadlock for the simple code to create and save additional profile fields.

// add a Google+ URL field to user profiles so they can get linked up properly for authorship

add_action( 'show_user_profile', 'click_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'click_show_extra_profile_fields' );
function click_show_extra_profile_fields( $user ) { ?>

<h3>Google+ Authorship</h3>

<table class="form-table">

<tr>
<th><label for="google_plus_authorship">Google+ URL</label></th>

<td>
<input type="text" name="google_plus_authorship" id="google_plus_authorship" value="<?php echo esc_attr( get_the_author_meta( 'google_plus_authorship', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Enter the full URL to your Google+ page. Should look like <b>https://plus.google.com/+NathanSwartzPGH/posts</b> or <b>https://plus.google.com/101851069402185145885/posts</b></span>
</td>
</tr>

</table>
<?php }

add_action( 'personal_options_update', 'click_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'click_save_extra_profile_fields' );

function click_save_extra_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) )
return false;

/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
update_usermeta( $user_id, 'google_plus_authorship', $_POST['google_plus_authorship'] );
}

// create a function to display the Google+ author link

function click_the_google_plus_author() {
global $post;
$author_id=$post->post_author;
$google_plus_page = get_the_author_meta( 'google_plus_authorship', $author_id );
if ($google_plus_page != '') {
echo '<link rel="author" href="'.$google_plus_page.'" />';
}
}

Next we need to add the functions to our theme. Open up your header.php and drop this somewhere in the <head> tag.

<?php click_the_google_plus_author(); ?>

Finally, go into WordPress and edit your profile, pasting the URL to your Google+ page in the new field there.

Enjoy!

Up Next: The Ultimate Meta Description Function for WordPress