0

I've found a few solutions to this but I am echoing some html and I have not been able to put those solutions (to return intead of echo, for example) to use. This is my function:

function get_loggedin_avatar (){global $current_user;
    if ( is_user_logged_in() ):
        get_currentuserinfo();  
        echo '<a href="/myaccount">';
        echo get_avatar( $current_user->ID, 96, left );
        echo '</a>';
    endif;  }
add_shortcode( 'loggedinavatar', 'get_loggedin_avatar' );

I see if I change all the echoes to returns, the avatar is output in the right spot but the link html stays at the top of the page.

I thought that perhaps making the html into variables that could be returned might work but I couldn't get that to work. Output buffering (or at least my attempt at it) did not work either.

** this is not a duplicate of this as output buffering did not work with my specific example.

1
  • Only partially - I noted that I tried the solution in that post unsuccessfully. Commented Jun 10, 2015 at 21:17

1 Answer 1

7

Shortcode callback should return it's output and not echo it.

Something like this should work just fine:

function get_loggedin_avatar (){
    global $current_user;
    $result = '';

    if ( is_user_logged_in() ) {
        get_currentuserinfo();  
        $result = '<a href="/myaccount">'.
            get_avatar( $current_user->ID, 96, left ).
            '</a>';
    }
    return $result;
}
add_shortcode( 'loggedinavatar', 'get_loggedin_avatar' );
0

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