Skip to content

Instantly share code, notes, and snippets.

@jdevalk
Last active December 17, 2015 01:28
Show Gist options
  • Save jdevalk/5528160 to your computer and use it in GitHub Desktop.
Save jdevalk/5528160 to your computer and use it in GitHub Desktop.
Gravatar comment verification...
<?php
/**
* Check whether a comment author is using a valid gravatar email address, if he/she is, approve the comment.
*
* @param bool $approved Whether or not the comment is approved alreadt.
* @param array $comment The entire comment object.
*
* @return bool $approved
*/
function yst_check_avatar_exists( $approved, $comment ) {
if ( !$approved ) {
// The input is expected to be slashed, so strip those and then do an md5 calc.
$hash = md5( stripslashes( $comment['comment_author_email'] ) );
// ?d=404 makes gravatar return a 404 when the gravatar doesn't exist, so non-existent gravatars will not return a 200.
$resp = wp_remote_head( 'https://secure.gravatar.com/avatar/'. $hash . '?d=404' );
// If the return code was a 200, the gravatar must exist, so approve the comment.
if ( !is_wp_error( $resp ) && $resp['response']['code'] == 200 )
$approved = 1;
}
return $approved;
}
add_filter( 'pre_comment_approved', 'yst_check_avatar_exists', 10, 2 );
@thefrosty
Copy link

I like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment