[resolved] get_avatar shows the same avatar for all users (6 posts)

  1. edow
    Posted 1 year ago #

    I have the following code to show the user avatars in the comment section:
    <img src=\"".get_avatar_url(get_avatar( $comment->comment_author_email, 50 ))."\" class=\"tooltip\" title=\"$like->comment_author\">

    My functions file contains this code so it returns the avatar url only:

    function get_avatar_url($get_avatar){
        preg_match("/src='(.*?)'/i", $get_avatar, $matches);
        return $matches[1];

    I use the plugin https://wordpress.org/plugins/simple-local-avatars/ to upload a custom avatar.

    The plugin is working great, but with the above code every user avatar in the comment section shows the same avatar instead of their own avatar. If I look in the admin section the avatars showing the right avatar for each user.

    Can anyone help me with this. I can't find out what I'm doing wrong.

    Even <?php echo get_avatar( $id_or_email, $size, $default, $alt ); ?> isn't working.

    This is the rest of the code which shows the comments:

    <div class="post-comments">
    			<div class="upvoters" id="votes">
    			<?php $comments = get_comments( array('post_id' => get_the_ID(),) );
    			$likes = array();
    			$unlikes = array();
    			foreach( $comments as $comment ) {
    				if('unlike' == $comment->comment_content ) $unlikes[] = $comment;
    					else $likes[] = $comment;
    			echo '<h3>'.count($likes).' bezoekers vinden dit leuk</h3>';
    			echo '<div class="upvoter-img">';
    			foreach( $likes as $like )
    				echo("<img src=\"".get_avatar_url(get_avatar( $comment->comment_author_email, 50 ))."\" class=\"tooltip\" title=\"$like->comment_author\">");
    			echo '</div>';
    			<div class="downvoters">
    			echo '<h3>'.count($unlikes).' bezoekers vinden dit niet leuk</h3>';
    			echo '<div class="downvoter-img">';
    			foreach( $unlikes as $unlike )
    				echo("<img src=\"".get_avatar_url(get_avatar( $comment->comment_author_email, 50 ))."\" class=\"tooltip\" title=\"$unlike->comment_author\">");
    			echo '</div>';
    			<div class="clearBoth"></div>

    Hopefully anyone can help me out.

  2. johnyson485
    Posted 1 year ago #

    I have been facing same issue on one of the installations.

  3. Ashok WPMU
    WPMU DEV Support Staff
    Posted 1 year ago #

    @johnyson485: Would you please create a new topic for you as per forum rule?

    @edow: Do you have any filter applied in get_avatar in your theme or in any plugin? If you don't use Simple Local Avatar, does it work?

  4. edow
    Posted 1 year ago #

    Hi Ashok,

    Thank you for your reply. If I deactivate the "Simple Local Avatars" plugin then it only shows the default avatar for every user (this one: http://1.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=50).

    If I activate the plugin all the avatars have the same local avatar as me (the admin). Even the not logged in users who give a comment. But this is only when the admin gave a comment. If not, the default avatars shows.

    The only code besides my already mentioned code is this in my function file:

    /* Prevent more than one %like */
    add_filter('pre_comment_approved','aigoo_one_vote',10 ,2);
    function aigoo_one_vote($approved, $commentdata) {
    	global $wpdb;
    	extract($commentdata, EXTR_SKIP);
    	// expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
    	$dupe = $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ", wp_unslash( $comment_post_ID ), wp_unslash( $comment_parent ), wp_unslash( $comment_author ) );
    	if ( $comment_author_email )
    		$dupe .= $wpdb->prepare( "OR comment_author_email = %s ", wp_unslash( $comment_author_email ) );
    	$dupe .= $wpdb->prepare( ") AND comment_content LIKE %s LIMIT 1", '%like' );
    	if ( $wpdb->get_var($dupe) ) {
    		if ( defined('DOING_AJAX') )
    			die( __('Je hebt hier al op gestemd') );
    		wp_die( __('Je hebt hier al op gestemd') );

    But I don't think this has something to do with it.

    As far as I know I don't have any filter applied. Better WP Security, Simple Local Avatars and Theme Check are all the plugins I use. I made the theme myself and with almost the exact same code it worked on another domain.

    If I only use <?php echo get_avatar( $id_or_email, $size, $default, $alt ); ?> it also only shows the default avatar instead of the local avatar. The reason why I use get_avatar_url id that I want to add a class and a title tag to the image.

  5. edow
    Posted 1 year ago #

    I found out. All I had to do was to change $comment->comment_author_email to $like->comment_author_email and $unlike->comment_author_email.

  6. edow
    Posted 1 year ago #


Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.