Skip to:
Content
Pages
Categories
Search
Top
Bottom

Help with new function

  • @devweb

    Participant

    Hello

    I created a new function in wpmu-functions.php to allow me to display usernames in the member directory search results:

    function  get_user_name_display( $username ) {
    global $wpdb;
    return $wpdb->get_results( $wpdb->prepare("SELECT user_login FROM $wpdb->users WHERE u.ID = %s", $username) );
    }

    I then call it in the members-loop.php file:

    <div class="item-meta">Username: <?php echo get_user_name_display( $username ); ?></div>

    Unfortunately, all it’s outputting is the word ‘array’ – obviously I’m doing something wrong (unsurprisingly) – but what, I have no idea!

    Can someone help please?

    Thanks very much

Viewing 18 replies - 1 through 18 (of 18 total)
  • @tiptap

    Participant

    just a guess but is it get_row you need?

    @devweb

    Participant

    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?

    @tiptap

    Participant

    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

    @jeffsayre

    Participant

    Okay, several issues to discuss here:

    1. 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.

    2. The user ID field does not contain string content. So:

      u.ID = %s

      should be

      u.ID = %d

    3. 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.

    @tiptap

    Participant

    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

    @jeffsayre

    Participant

    Tiptap-

    Make sure that you declare bp as global in your index.php file:

    global $bp;

    @tiptap

    Participant

    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

    @jeffsayre

    Participant

    @Tiptap

    You’re welcome!

    @devweb

    Participant

    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

    @jeffsayre

    Participant

    @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;

    @devweb

    Participant

    Thanks Jeff

    Made the alteration, still outputting ‘Array ( )’ though?

    Thanks, DW

    @jeffsayre

    Participant

    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.

    @devweb

    Participant

    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

    @jeffsayre

    Participant

    So, you are simply trying to customize that output of the search results?

    @devweb

    Participant

    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?

    @jeffsayre

    Participant

    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.

    @devweb

    Participant

    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

    @jeffsayre

    Participant

    @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.

Viewing 18 replies - 1 through 18 (of 18 total)
  • The topic ‘Help with new function’ is closed to new replies.
Skip to toolbar