Support » Fixing WordPress » Get_avatar returning empty or gravatar.com URL

  • Hi there!!
    I’m developing my theme on a WordPress 4.6 platform installed on localhost using XAMPP 5.6.3.

    Developing my single.php page, I want the Author Avatar to be displayed. The file hierarchy is as following:

    index.php -> single.php -> content.php (I’ve content types enabled and content.php is used as default template for posts. In single.php there is the loop and in content.php the content part called by get_template_part(‘content’, get_post_format);

    This is the content.php code:

    <article id="post-<?php the_id();?>" <?php post_class('post-single');?> >
        <header class="header_post">
            <?php the_title(sprintf('<H1><a href="%s" rel="bookmark" class="entry-title">', esc_url( get_permalink() )), '</a></H1>'); ?>
    
            <FIGURE class="featured-image-with-text">
    
                  <?php mycustom_post_thumbnail('full','img-responsive center-block'); ?>
    
            </FIGURE>
                <?php the_excerpt(sprintf('<H2>', '%s', '</H2>')); ?>
    
            <div class="entry-meta-box entry-meta text-center">
                <?php mycustom_author_avatar(); ?>
                <div class="meta-author"><span class="glyphicon glyphicon-bullhorn "></span> <?php the_author(); ?> | <?php the_date("d/m/Y");?></div>
            </div>
        </header>
    
        <div class="limiter"></div>
    
        <div class="entry">
            <?php    
    
            the_content();
        ?>
        </div>
    
    </article>

    The problem is when I call the function mycustom_author_avatar. The code of the function is the following:

    function mycustom_author_avatar() {
         //getting the ID of post/page's Author
         $author_id = get_the_author_meta('ID');
         // sets the fallback Avatar
         $default =  get_template_directory_uri() .'/img/logo-icon.png';
         $args = array (
             'class' => 'img-circle dropshadow img-center',
             'size' => 96,
             'default' => $default
         );
    
         echo get_avatar($author_id,$args);
    }

    the function doesn’t show any avatar. In a early implementation it had been returning the Author ID as string. Then, looking at the HTML output I saw that the <IMG SRC> generated by get_avatr() contains a link pointing to http://1.gravatar.com/avatar/13eb0bc0c3d1b3db35b3aa996e494042?s=1&d=mm&r=g instead of the expected local avatar url or fallback image url.

    Has anyone a suggestion ? I can’t really understand and figure out a solution.

    Thanks
    Simone

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi, Simone (@simonefavaro). Try this modified version of your mycustom_author_avatar function and see if you get a better result:

    function mycustom_author_avatar() {
         //getting the ID of post/page's Author
         $author_id = get_the_author_meta('ID');
         $size = 96; // 96 is already the default
         // sets the fallback Avatar
         $default =  get_template_directory_uri() .'/img/logo-icon.png';
         $args = array (
             'class' => 'img-circle dropshadow img-center',
         );
    
         echo get_avatar( $author_id, $size, $default, $alt, $args );
    }
    Thread Starter simonefavaro

    (@simonefavaro)

    Super!! It works!! Thanks a lot girlieworks!
    I was wondering why get_avatar doesn’t accept ‘size’ and ‘default’ as terms in array. Reading codex, I understood they cab be part of $args array…

    Anyway… thanks so much!

    Thread Starter simonefavaro

    (@simonefavaro)

    Now it seems there is a glitch with the fallback image.

    Even if the destination URL is correct and the image is stored at the declared directory, the localhost server responds as if it is a broken link. I tried to load the image as a custom avatar in Settings>discussions using the well known hook and even in dashboard WordPress responds as a broken link.

    I read around that it’s a well know problem with localhost installation but I wonder if there is a way to have it working even in the localhost installation.

    the get_avatar() function indeed doesn’t call directly the image. It points at a gravatar.com address and then redirects to your URL. For some reasons it works when the user has an avatar set but it doesn’t when the callback avatar is called.

    The Url appears as something like that:

    http://0.gravatar.com/avatar/0662dff0cbc7c4d344063d643429dd41?s=96&d=http%3A%2F%2Flocalhost%2Fnuovolevantino3%2Fwp-content%2Fthemes%2Fnuovolevantino%2Fimg%2Flogo-nuovo-levantino-icon.png&r=g

    It’s clear that a redirect to a localhost server wouldn’t never work. I wonder why WordPress needs such a workaround to display an image stored in a local directory.

    I don’t want to use a plugin (that works). I want the solution coded in my template.

    Have you any suggestion?

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get_avatar returning empty or gravatar.com URL’ is closed to new replies.