0

I have a custom comment form and any one can add comments without creating an account. Now I want to set comment author image for each of the comment. I could not find any function or support for this as mostly there is support for registered users. But I am allowing unregistered users to add comments as well. Is there any way to do this ?

2
  • That means anonymous user could choose the avatar and then you must save it to somewhere in database so that it will be retrieved next time, right? – simongcc May 16 '20 at 0:47
  • Yes you are right. I am already uploading an image in media and saving this image as comment meta and than displaying this on front end. But it is not displaying in admin panel where we normally display avatar. Can we use this as avatar ? – wpdd May 16 '20 at 0:50
0

I am also interested and after spending hours to test, read source code and some online docs. Finally understand how it is working. And end up the method is easy but not obvious.

Not sure how you add the image, you may modify the code to meet your actual situation and here is the WordPress way to add custom Gravatar. The example use a dummy place holder which you could replace your own gravatar.

The Avatar icons in WP is fetching from gravatar.com basically.

Add options for backend

// a WordPress way to add avatar for non-registered user/anonymous
// add options in settings -> discussion
add_filter( 'avatar_defaults' , 'ws366726_avatar_defaults' );
function ws366726_avatar_defaults($avatar_defaults) {
    // you may fetch the meta option for the image
    // the key is the path to image for Gravatar based on http://en.gravatar.com/site/implement/images/
    // this key will be loaded as default if image is not found for specific user in Gravatar database based on email lookup

    // here, you may change to your image saved in meta
    $avatar_defaults['https://via.placeholder.com/64'] = 'My Default';

    return $avatar_defaults;
}

Gravatar image: http://2.gravatar.com/avatar/52b752660072401e4a971105206d44a0?s=32&d=mm&f=y&r=g where mm is Mystery Man

Custom image example by changing the parameter in d: http://2.gravatar.com/avatar/52b752660072401e4a971105206d44a0?s=32&d=https://via.placeholder.com/32?ssl=1&f=y&r=g

This is how WordPress fetch the default gravatar image.

remove default gravatar rendering in backend comment list

// in hook 'admin_bar_menu', the 'comment author' will be available for removal
add_action ('admin_bar_menu' , 'ws366726_remove_default_avatar_in_comment_list');
function ws366726_remove_default_avatar_in_comment_list() {
    global $wp_filter;

    // because 'comment_author' by default defined inside a class, need to find it out and remove_filter
    foreach ($wp_filter['comment_author'][10] as $key => $value) {
        if( preg_match('#floated_admin_avatar#', $key) ) {
            remove_filter( 'comment_author', array( $wp_filter['comment_author'][10][$key]['function'][0],'floated_admin_avatar' ), 10, 2);
        }
    }
}

render custom gravatar for backend comment list

add_filter( 'comment_author' , 'ws366726_comment_author', 11, 2 );
function ws366726_comment_author( $name ) {
    // display default options instead, I think core team could implement it instead of a hard coded 'mystery' in the future so that developer don't have to remove and add back the similar things
    $default = get_option( 'avatar_default', 'mystery' ); // load the saved default in DB, by default, it is mystery
    $avatar = get_avatar( get_comment(), 32, $default );
    return "$avatar $name";
}

This way of adding Avatar let you control the default avatar in the same settings menu settings -> discussion. So it preserves the original flexibility.

The principal behind is that gravatar are prepared by get_avatar() function which by default get the option of anonymous avatar from option with get_option( 'avatar_default', 'mystery' ) which serves mystery as default if not saved before.

The key mystery is where to look for the image when it creates url to link to the gravatar.com. According to this post, it could be custom url for loading as default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.