WordPress comments add input mailbox instantly display Gravatar avatar function

Gravatar is an abbreviation for Globally Recognized Avatar, a service launched by gravatar, meaning “Global Universal Avatar” (already wall). If you put your own avatar on Gravatar’s server, then in any message that supports Gravatar’s blog or message, just provide the email address associated with this avatar to show your Gravatar header.
New theme with this feature, it is not difficult to achieve, so posted to share under
1. Add the following code to the functions.php file and add the default avatar to the comment box.
add_filter (‘comment_form_top’, ‘show_gravatar’);
function show_gravatar () {
global $ current_user;
get_currentuserinfo ();
echo is_avatar ($ current_user-> user_email, 40); // 40 is the size of the avatar, step 4 is the same
}
2. Set the position of the avatar with CSS, which is fixed in the upper right corner.
#commentform {position: relative}
#commentform .avatar {position: absolute; top: 0; right: 20px}
3. Download http://pajhome.org.uk/crypt/md5/2.2/md5-min.js, save to the theme folder.
4. Rename the file just to gravatar.js, add the following code at the end of this file:
if (document.getElementById (“email”)) {
document.getElementById (“email”). onblur = function () {
if (/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\a-a- zA-Z0-9_-]{2,3} ) {1,2}) $ /. Test (this.value)) document.getElementById (“commentform”). GetElementsByTagName (“img”) [0] .src = “http://www.gravatar.com/avatar / “+ Hex_md5 (this.value) +”? D = mm & s = 40 “;
};
};
If you find the email input box, when the cursor focus out of the email input box, with the regular test input is the email format, if it is inserted before the picture address to the newly generated address.
5. Open footer.php, add the following code:
<? php if (is_singular () && comments_open ()) {?> // Judgment is not a separate post page and comments open
<script type = “text / javascript” src = “<? php bloginfo (‘template_url’);?> / js / gravatar.js”> </ script>
<? php}?>
Simple 5 steps, instant display Gravatar function on the complete realization.