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 ???
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.
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' );
And if that works for you I will look at the profile info shortcode later.
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.
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' );
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/
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,
) );
}
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,
) );
}