Last active
August 29, 2015 14:03
-
-
Save gregrickaby/a394452552832a07c098 to your computer and use it in GitHub Desktop.
How to create an Author Box using the Gravatar API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// DO NOT INCLUDE OPENING PHP TAG AND PLACE INTO FUNCTIONS.PHP | |
/** | |
* Get Author's Gravatar profile data. | |
*/ | |
function custom_get_gravatar_profile() { | |
// Get author data | |
$author = strtolower( trim( get_the_author_meta( 'email' ) ) ); | |
$author_ID = get_the_author_meta( 'ID' ); | |
// Check for transient cache | |
if ( false === ( $gravatar_profile = get_transient( 'gravatar_profile_' . $author_ID ) ) ) { | |
// Check if author has a Gravatar | |
$response = wp_remote_get( 'http://www.gravatar.com/' . md5( $author ) . '.php' ); | |
// Did we recieve profile info? | |
if ( ! 200 == wp_remote_retrieve_response_code( $response ) ) { | |
return '<div id="message" class="error"><p>' . sprintf( __( 'This user does not have a Gravatar! Why not <a href="%s">set up a profile</a>?', 'textdomain' ), 'http://en.gravatar.com/' ) . '</p></div>'; | |
} | |
// Parsing profile data from Gravatar response | |
$body = unserialize( wp_remote_retrieve_body( $response ) ); | |
// Is our data in an array? | |
if ( ! is_array( $body ) ) { | |
return '<div id="message" class="error"><p>' . __( 'There was an error extracting the profile from Gravatar\'s API. Please try again later.', 'textdomain' ) . '</p></div>'; | |
} | |
// Shift the profile data, which is two levels deep into an array | |
$gravatar_profile = current( current( $body ) ); | |
// Place profile in a transient, expire after 12 hours | |
set_transient( 'gravatar_profile_' . $author_ID, $gravatar_profile, 12 * HOUR_IN_SECONDS ); | |
} | |
// Return the profile | |
return $gravatar_profile; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array | |
( | |
[id] => 3631430 | |
[hash] => 28af3e39c0a1fe4c31367c7e9a8bcac3 | |
[requestHash] => 28af3e39c0a1fe4c31367c7e9a8bcac3 | |
[profileUrl] => http://gravatar.com/gregrickaby | |
[preferredUsername] => gregrickaby | |
[thumbnailUrl] => http://1.gravatar.com/avatar/28af3e39c0a1fe4c31367c7e9a8bcac3 | |
[photos] => Array | |
( | |
[0] => Array | |
( | |
[value] => http://1.gravatar.com/avatar/28af3e39c0a1fe4c31367c7e9a8bcac3 | |
[type] => thumbnail | |
) | |
) | |
[currency] => Array | |
( | |
) | |
[profileBackground] => Array | |
( | |
[color] => # | |
[url] => https://secure.gravatar.com/bg/3631430/d3a13611c357b6cfe4945d329ea2f175 | |
) | |
[name] => Array | |
( | |
[givenName] => Greg | |
[familyName] => Rickaby | |
[formatted] => Gregory James Rickaby | |
) | |
[displayName] => Greg | |
[aboutMe] => Design Lead @WebDevStudios. Audio Engineer @WiregrassChurch. Amateur pizza chef and lover of a good brew. | |
[currentLocation] => Enterprise, Alabama | |
[accounts] => Array | |
( | |
[0] => Array | |
( | |
[domain] => facebook.com | |
[display] => gregrickaby | |
[url] => http://www.facebook.com/gregrickaby | |
[username] => gregrickaby | |
[verified] => true | |
[shortname] => facebook | |
) | |
[1] => Array | |
( | |
[domain] => profiles.google.com | |
[display] => profiles.google.com | |
[url] => http://profiles.google.com/115960795578445596136 | |
[userid] => 115960795578445596136 | |
[verified] => true | |
[shortname] => google | |
) | |
[2] => Array | |
( | |
[domain] => twitter.com | |
[display] => @GregRickaby | |
[url] => http://twitter.com/GregRickaby | |
[username] => GregRickaby | |
[verified] => true | |
[shortname] => twitter | |
) | |
[3] => Array | |
( | |
[domain] => youtube.com | |
[display] => gregrickaby | |
[url] => http://www.youtube.com/user/gregrickaby | |
[username] => gregrickaby | |
[verified] => true | |
[shortname] => youtube | |
) | |
) | |
[urls] => Array | |
( | |
[0] => Array | |
( | |
[value] => http://gregrickaby.com | |
[title] => My Blog | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment