Skip to:
Content
Pages
Categories
Search
Top
Bottom

How do I display a single user’s avatar with just the username NOT user-id?

  • @trey96

    Participant

    I would like to display the following information on any page using a shortcode and PHP code:

    Profile image/avatar (using separate shortcode)

    Bio/description (using separate shortcode)

    Please note: I would like to do so using just the username associated with an account and show the information publicly from a visitor’s perspective (without logging in). The page is for visitors to see.

    Thank you in advance.

Viewing 9 replies - 1 through 9 (of 9 total)
  • @thinlizzie

    Participant

    Hi

    Where are you getting the username from?
    If this is to be displayed to visitors, then clearly it’s not the logged-in user.
    Should it show different info in different contexts, or it’s the same thing every time ie. show the info for @johnsmith ???

    @trey96

    Participant

    Hi @thinlizzie,

    Yes, that is correct. It won’t be from the logged-in user. Currently, I am pulling the username from a page url. The username is located at the very end of that url. Here is an example of what it would look like for @johnsmith:

    http://www.samplewebsite.com/bpmembers/johnsmith

    *The url structure above does not change based on login status. The url would be exactly the same for both logged-in users and visitors (not logged in) viewing that page.

    I want to pull information from any user’s account that is stored in my site’s database (profile image, bio, etc.) and use separate shortcodes to display each piece of information on any page I create in my WordPress admin. Also, the information will not be different, but display the same profile information regardless of login status. So a visitor or logged in user would see the same profile information.

    I hope I can achieve this using PHP codes and custom shortcodes that look something like this:

    Examples:

    [show avatar username= “johnsmith”]

    [show description username= “johnsmith”]

    This way, I can manually swap out the username within the quotations to display information for any user’s profile.

    @thinlizzie

    Participant

    Try this for your avatar display.

    Your shortcode syntax would look like …

    [showavatar username=”johnsmith”]

    You can change width & height to your preference.

    
    function show_avatar ($atts) {
    
    $user = get_user_by( 'login' , $atts['username'] );
    	
    return bp_core_fetch_avatar( array(
      'item_id'	 => $user->ID,
      'width'	 => 50,
      'height'	 => 50,
      ) );
    }
    
    add_shortcode( 'showavatar' , 'show_avatar' );
    
    

    @thinlizzie

    Participant

    And if that works for you I will look at the profile info shortcode later.

    @trey96

    Participant

    Yes, it definitely works. The only issue I am having is with adjusting the width and height. When I change it from 50 to 80 within the PHP code, the image dimensions still appear to be 50px by 50px.

    Also, I would still like to pull the user’s bio using a separate shortcode using the username. Thanks so much.

    @thinlizzie

    Participant

    Assuming you have a Buddypress profile field for your users bio (in this case called ‘Description’), this will work.

    
    function show_description ($atts) {
    
    $user = get_user_by( 'login' , $atts['username'] );
    	
    $args = array(
         'field'   => 'Description',
         'user_id' => $user->ID
    );
    
    return bp_get_profile_field_data( $args );
    
    }
    
    add_shortcode( 'showdescription' , 'show_description' );
    

    @thinlizzie

    Participant

    Re. avatar size, not sure, you can try putting a span around your shortcode, give it a class and with CSS try to force the avatar size.

    Or you can add a class into the fetch_avatar array and try that way.

    See: https://www.buddyboss.com/resources/reference/functions/bp_core_fetch_avatar/

    @thinlizzie

    Participant

    Also re. Avatar size, first try adding ‘type = full’ into the array, should allow you to specify larger sizes, as below …

    
    return bp_core_fetch_avatar( array(
      'type' 	 => 'full'
      'item_id'	 => $user->ID,
      'width'	 => 80,
      'height'	 => 80,
      ) );
    }
    
    

    @thinlizzie

    Participant

    I forgot the comma above, should be a comma after ‘full’ , like so …

    
    
    return bp_core_fetch_avatar( array(
      'type' 	 => 'full',
      'item_id'	 => $user->ID,
      'width'	 => 80,
      'height'	 => 80,
      ) );
    }
    
    
Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Skip to toolbar