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)?

1 Answer
1

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.

Leave a Reply

Your email address will not be published. Required fields are marked *