just a guess but is it get_row you need?
Well that removed the ‘array’ text, but there is now nothing appearing, thanks for pointing that out, I believe it is supposed to be ‘get_row’!
Any more ideas on what I’ve got wrong?
I had similar that needed that recently.
Have you tried
global $wpdb;
$row = $wpdb->get_results( $wpdb->prepare(“SELECT user_login FROM $wpdb->users WHERE u.ID = %s”, $username) );
print_r($row);
I think you access each individual column like this
$row[“coloumRow”];
see if this helps https://codex.wordpress.org/Function_Reference/wpdb_Class
Okay, several issues to discuss here:
-
I created a new function in wpmu-functions.php
Why modify a core WPMU file to be used in a BuddyPress theme file? Instead, create this function in your bp-custom.php file. That why, when you upgrade BP, your changes will not be lost–assuming that you do not delete your bp-custom.php file.
- The user ID field does not contain string content. So:
u.ID = %s
should be
u.ID = %d
- What you are after is grabbing a member’s ID and then using that to pull their login name (username). I don’t think that the variable $username is available for the function call–at least not the way you are trying to reference it. I would use this instead:
$bp->displayed_user->id
You will need to declare bp as global in your members-loop.php file.
Jeff that sounds more logical.
and in fact i was trying to get that to work last night.
why would it be <?php echo $bp->displayed_user->id; ?> added into /profile/index.php doesn’t output the id?
Thanks
Tiptap-
Make sure that you declare bp as global in your index.php file:
global $bp;
ahhhhh you’d don’t know how long I stayed up trying to work out how to get it to work.
adding the global got it working.
Thanks hugely
Wow, thanks guys, got a bit lost in your convo though, so I have this (bear in mind my knowledge is a bit pants), I have used the bp-custom.php file as you suggested Jeff:
In the bp-custom.php file:
<?php
function get_user_name_display() {
global $wpdb;
$row = $wpdb->get_results( $wpdb->prepare("SELECT user_login FROM $wpdb->users WHERE u.ID = %d", $bp->displayed_user->id) );
print_r($row);
}
?>
Then in the member-loop.php file:
<?php global $bp; echo get_user_name_display( $bp->displayed_user->id ); ?>
This is currently outputting ‘array ( )’. I think I’m nearly there, just need a little shove …
Thanks again
@Devweb
In your get_user_name_display() function, you also need to declare $bp as global, otherwise it will not be able to pull the data from $bp->displayed_user->id.
So, do this:
function get_user_name_display() {
global $wpdb, $bp;
Thanks Jeff
Made the alteration, still outputting ‘Array ( )’ though?
Thanks, DW
Why are you not using the function bp_core_get_user_displayname() in bp-core.php? You can pass it the user’s ID and it will pull the username for you.
But, here’s what you need to do with your code:
$row = $wpdb->get_var( $wpdb->prepare("SELECT user_login FROM $wpdb->users WHERE id = %d", $bp->displayed_user->id) );
Also, note that this will only work when a viewer is on a member’s page as it pulls the ID of just that displayed user.
Jeff, just tried the function bp_core_get_user_displayname() and its throwing me a ‘undefined function’ error, I think this is why I didn’t use it in the first place.
Tried the above code, but unfortunately it didn’t work, I think this is because I am calling it on the member search page rather than a profile page. Where you have the ajax search and it displays the members, I want it so that the ‘username’ is also displayed per user as well as their ‘display name’ or full name.
I’m going to have a fiddle with the ‘get_var’ variable as I’ve seen this on the WP function reference but not tried it.
Obviously, if you can advise any more I would greatly appreciate it!
Thanks again
So, you are simply trying to customize that output of the search results?
Yes, so not just the member name ( from function bp_the_site_member_name() ) but underneath that the username too.
I take it I’m being stupid and this is actually a lot more simple than I thought?
In your member-loop.php file you can add this
<?php echo bp_core_get_username( bp_get_the_site_member_user_id() ) ?>
to this line:
<div class="item-title"><a href="<?php bp_the_site_member_link() ?>"><?php bp_the_site_member_name() ?></a></div>
You do that right before the closing “a” tag. But, just remember that whenever you do a theme upgrade, you’ll lose your customizations. This, of course, is assuming that you are using the default member’s theme that comes with BP.
You will have to do your own styling to output the user’s login where you want it to be positioned.
JEFF SAYRE – I. LOVE. YOU.
Thank you so so so very much, I’ve been trying to do this for months, you have no idea how relieved I am. I’m going to go and have a nice calming cigarette outside while I cry with relief!
Again, thank you so much.
Regards
DW
@Devweb
Haha! You’re welcome!
I assume that your issue has been resolved. So, all you need to do now is set the light on top to green and we’re good to go.