2

I am trying to replace Wordpress comment author data:

1) Avatar (uploaded image instead of Gravatar)

2) Author link (link to author page as only members can comment)

I have found a great solution to this from this question, and have implemented the following code:

if ( ! function_exists( 't5_comment_uri_to_author_archive' ) )
{
add_filter( 'get_comment_author_url', 't5_comment_uri_to_author_archive' );

function t5_comment_uri_to_author_archive( $uri )
{
    global $comment;

    // We do not get the real comment with this filter.
    if ( empty ( $comment )
        or ! is_object( $comment )
        or empty ( $comment->comment_author_email )
        or ! $user = get_user_by( 'email', $comment->comment_author_email )
    )
    {
        return $uri;
    }

    return get_author_posts_url( $user->ID );
}
}

The code works perfect for replacing links, and I want to use it to replace the avatars as well. I have created a copy of the function, and changed the names and the return:

if ( ! function_exists( 'my_comment_imgs' ) )
{
add_filter( 'get_comment_author_url', 'my_comment_imgs' );

function my_comment_imgs( $uri )
{
    global $comment;

    // We do not get the real comment with this filter.
    if ( empty ( $comment )
        or ! is_object( $comment )
        or empty ( $comment->comment_author_email )
        or ! $user = get_user_by( 'email', $comment->comment_author_email )
    )
    {
        return $uri;
    }

    return get_avatar( $user->ID );
}
}

However, this function negates the first one, so I get updated avatars, but loose the author links. How do I replace both elements at the same time (avatars and links)?

  • Where are the avatars associated? (This is not standard WP functionality, is it?) Anyway, you need to add a filter on get_avatar or even pre_get_avatar, not get_comment_author_url – kero Jul 4 '17 at 14:12
  • I tried that, but when I add get_avatar (according to this) it doesn't work, the site won't even load properly. – fjanecic Jul 4 '17 at 14:28
  • the get_avatar filter is designed to do what you want to do, while the get_comment_author_url filter isn't,. Perhaps you should add all of it to your question, so that we can see what's wrong with it - why it's crashing your site. – CK MacLeod Jul 4 '17 at 23:18
  • You guys were right, fiddled with the code again and managed to get it working properly. Thanks for the nudge in the right direction! – fjanecic Jul 5 '17 at 10:54
0

Maybe I was burned out from work before, but this morning I took another go at the same code and managed to get it running properly.

if ( ! function_exists( 'comment_imgs' ) )
{
add_filter( 'get_comment_author_url', 'comment_imgs' );

function comment_imgs( $avatar, $id_or_email, $size, $default, $alt )
{
    global $comment;

    // We do not get the real comment with this filter.
    if ( empty ( $comment )
        or ! is_object( $comment )
        or empty ( $comment->comment_author_email )
        or ! $user = get_user_by( 'email', $comment->comment_author_email )
    )
    {
        return $uri;
    }

    return get_avatar( $user->ID );
}
}

Credit for the original code goes to Thomas Scholz for linking to author pages, this modification retrieves local avatars and two functions don't clash with each other.

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.