google_authorship.module in Google Authorship 7
Same filename and directory in other branches
A module to display Google+ profile pictures of node authors in Google search results.
This module adds a field to the user entity for a link the the user's Google+ profile. Using that field, if it is filled, this module alters the By line of a node's full content display to link to the Google+ profile. Finally, by setting "rel='author'" Google will recognize and display an authors profile picture in its search results.
File
google_authorship.moduleView source
<?php
/**
* @file
* A module to display Google+ profile pictures of node authors in Google
* search results.
*
* This module adds a field to the user entity for a link the the user's
* Google+ profile. Using that field, if it is filled, this module alters the
* By line of a node's full content display to link to the Google+ profile.
* Finally, by setting "rel='author'" Google will recognize and display an
* authors profile picture in its search results.
*/
// The string to prefix the ID to make up the full URL.
define('GOOGLE_AUTHORSHIP_PREFIX', 'http://plus.google.com/');
/**
* Implements template_preprocess_username().
*
* Alters the 'name' variable to be a link to a Google+ profile and sets the
* "rel='author'" parameter on the generate URL.
*/
function google_authorship_preprocess_username(&$variables) {
// Checks if the loaded page is a node page.
$is_node_page = $node = menu_get_object('node');
// Checks whether author is a comment author.
$is_not_comment = !isset($variables['account']->comment_body);
// Checks to see if the author's Google+ ID is set, and if so, sets a variable
// for the id.
$is_id_set = $google_id = google_authorship_get_google_id($variables['uid']);
// Checks conditions set above and if it passes, alters the link path of the
// author.
if ($is_node_page && $is_not_comment && $is_id_set) {
$variables['link_path'] = GOOGLE_AUTHORSHIP_PREFIX . $google_id . "?rel=author";
}
}
/**
* Implements google_authorship_get_google_id().
*
* Returns a string containing the Google+ ID for a specified UID
*
* @param:
* $uid: The user id of the node author
*
* @return:
* A string with the Google+ ID of the node author, if it has been set or
* NULL if it hasn't
*/
function google_authorship_get_google_id($uid) {
// Loads the author's user account.
$account = user_load($uid);
// Checks if Google+ ID field is set for the user and creates array for items
// in field if so.
if ($items = field_get_items('user', $account, 'field_google_plus_id')) {
// Get the Google+ ID for the items set above.
$google_id = $items[0]['safe_value'];
}
else {
// If the Google+ ID field was empty, set $google_id to NULL.
$google_id = NULL;
}
// Return the Google+ ID if it existed and was a valid length,
// otherwise this will return NULL.
return $google_id;
}
/**
* Implements hook_form_alter().
*
* Adds additional validation to the user profile edit form.
*/
function google_authorship_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
$form['#validate'][] = 'google_authorship_form_validate';
}
/**
* Implements hook_form_validate().
*
* Verifies that the Google+ ID entered is a 21 digit numeric string.
*/
function google_authorship_form_validate(&$form, &$form_state) {
// If the user input data into field_google_plus_id.
if (!empty($form_state['values']['field_google_plus_id'][LANGUAGE_NONE][0]['value'])) {
// Get the form input for Google+ ID.
$google_id = $form_state['values']['field_google_plus_id'][LANGUAGE_NONE][0]['value'];
// If the string is not 21 digits or it is not a numeric string.
if (strlen($google_id) != 21 || !preg_match('/^\\d+$/', $google_id)) {
// Return for resubmission and display error.
form_error($form, t('The Google+ ID should be 21 numeric digits.'));
}
}
}
/**
* Implements hook_metatag_config_default_alter().
*
* Automatically assign the google_authorship_url to the 'author' meta tag.
*/
function google_authorship_metatag_config_default_alter(&$config) {
if (isset($config['node'])) {
$config['node']->config['author']['value'] = '[node:google_authorship_url]';
}
}
Functions
Name![]() |
Description |
---|---|
google_authorship_form_user_profile_form_alter | Implements hook_form_alter(). |
google_authorship_form_validate | Implements hook_form_validate(). |
google_authorship_get_google_id | Implements google_authorship_get_google_id(). |
google_authorship_metatag_config_default_alter | Implements hook_metatag_config_default_alter(). |
google_authorship_preprocess_username | Implements template_preprocess_username(). |
Constants
Name![]() |
Description |
---|---|
GOOGLE_AUTHORSHIP_PREFIX |