Skip to:
Content
Pages
Categories
Search
Top
Bottom

Getting group avatar, outside of loop.

  • I’ve found only a few examples of using bp_get_group_avatar(). I can’t seem to get anything returned other then a blank image.

    http://phpxref.ftwr.co.uk/buddypress/bp-groups/bp-groups-template.php.source.html#l380

    So from this method, I’ve tried passing an item_id in the args array as the group ID and also bypassing this wrapper function by calling “bp_core_fetch_avatar()” but without much luck. It would be nice to throw an error or give some feedback other then an unexpected result.

    I would be happy to fetch something from the database if it where there but my best guess without getting too far into the code is that when that fancy jquery cropper thing hands off the file somewhere, it saves it using an MD5 or similar. I’ve spent plenty of hours looking at this by now and if I can do this the right way, I would prefer that then hacking this the wrong way.

    Could anyone give me some pointers? I have a group ID within a plugin I’m writing, I just want to have it return the avatar or even better yet a URL or filename. Using a method that echos an image tag in a loop is not very useful to me. I really need a method that returns something, not echo’ed

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Alright, drilling down a bit further, in bp-groups/bp-grouptemplatetags.php, around line 279, there’s our bp_get_group_avatar() method.

    `
    function bp_get_group_avatar( $args = ” ) {
    global $bp, $groups_template;

    $defaults = array(
    ‘type’ => ‘full’,
    ‘width’ => false,
    ‘height’ => false,
    ‘class’ => ‘avatar’,
    ‘id’ => false,
    ‘alt’ => __( ‘Group avatar’, ‘buddypress’ )
    );

    $r = wp_parse_args( $args, $defaults );

    extract( $r, EXTR_SKIP );
    var_dump(‘buddypress group_avatar_call’, bp_core_fetch_avatar( array( ‘item_id’ => $groups_template->group->id, ‘object’ => ‘group’, ‘type’ => $type, ‘avatar_dir’ => ‘group-avatars’, ‘alt’ => $alt, ‘css_id’ => $id, ‘class’ => $class, ‘width’ => $width, ‘height’ => $height )) );
    /* Fetch the avatar from the folder, if not provide backwards compat. */
    if ( !$avatar = bp_core_fetch_avatar( array( ‘item_id’ => $groups_template->group->id, ‘object’ => ‘group’, ‘type’ => $type, ‘avatar_dir’ => ‘group-avatars’, ‘alt’ => $alt, ‘css_id’ => $id, ‘class’ => $class, ‘width’ => $width, ‘height’ => $height ) ) )
    $avatar = ‘group->avatar_thumb ) . ‘” class=”avatar” alt=”‘ . esc_attr( $groups_template->group->name ) . ‘” />’;

    return apply_filters( ‘bp_get_group_avatar’, $avatar );
    }
    `

    So I’ve var dumped the parameters that are used for bp_core_fetch_avatar() when it’s called from the default group landing page, then var_export($args, true) and copied that output directly to my own code and surprisingly, I get the same result as before, a blank img tag. So, this leads me to believe that this method is not meant to be used in the way I am using it.

    As I find a better solution, I will follow up in case this is useful to someone else.

    Solution: Getting the URL with bp_core_fetch_avatar() and using the option ‘html’ => false to get group images by URL rather then a whole image tag.

    IE:
    `
    $avatar_options = array ( ‘item_id’ => ’1′, ‘object’ => ‘group’, ‘type’ => ‘full’, ‘avatar_dir’ => ‘group-avatars’, ‘alt’ => ‘Group avatar’, ‘css_id’ => 1234, ‘class’ => ‘avatar’, ‘width’ => 50, ‘height’ => 50, ‘html’ => false );

    $result = bp_core_fetch_avatar($avatar_options);
    `

    Looking at this particular code makes me nervous about performance. Is it necessary to read from the database and the filesystem to get this type of resource? I sure hope there’s some caching going on somewhere.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.