:-{>

BuddyPress – Alphabetical and by Surname.

In Work on November 21, 2012 at 12:40 pm

I’ve been building a custom WordPress website for a client recently and been having a steep learning curve getting my head around the BuddyPress plugin. While a terrific app, giving you all the features of Facebook for my client’s users, it’s also very hard to find good documentation for the way the plugin works – such as getting various data elements. It took me a while to realise that you can actually pull out elements of data by simply adding ‘get’ to the request string, so for the avatar putting bp_member_avatar() would get you the members avatar image, but putting bp_get_member_avatar() would give you the full data string, not automatically echoed when returned.

Anyway, the problem was taking the members list and returning them in a alphabetical state, Buddypress’s default state is ‘active’, so finding a way of making them display as A-Z became a bit of a challenge,  searching various blogs via Google I found that you can add commands to alter the output, so on ‘members-loop.php‘, at the line that reads

if ( bp_has_members( bp_ajax_querystring( ‘members’ ) ) ) :

you can add

. ‘&type=alphabetical&per_page=40’ 

so that it becomes

if ( bp_has_members( bp_ajax_querystring( ‘members’ ) . ‘&type=alphabetical&per_page=40’ ) ) :

What this is doing now is telling BuddyPress to make the members list display as A-Z with 40 members per page instead of the default 20. The only trouble with this though is that it displays your members alphabetically by their first names – Adam Zebra, Bill Alpha, Charlie Woods instead of Bill Alpha, Charlie Woods, Adam Zebra. My solution was to break up the loop into an array and sort it alphabetically by last name.

You can also ‘hard-code’ this by going to the actual function found on ‘bp-members-template.php‘. Go to line 264 you see the function ‘function bp_has_members( $args = ” ) {‘ this is where you can set all the arguments (active ( default ) | random | newest | popular | online | alphabetical).

As I mentioned earlier, finding out how to get at various BuddyPress elements using the ‘get’ statement was vital for this.

Starting around line 30, here is my solution:

<?php

do_action( ‘bp_before_directory_members_list’ );

$members_list_array = array();

while ( bp_members() ) : bp_the_member();

$name_temp = explode(” “, bp_get_member_name());
if ($name_temp[1]==””) { $name_temp[1] = $name_temp[0];}

array_push($members_list_array, array( “link” => bp_get_member_permalink(), “full” => bp_get_member_name(), “icon” => bp_get_member_avatar(), “first” => $name_temp[0], “last” => $name_temp[1] ) ); 

endwhile;

foreach($members_list_array as $key => $row) { $atoz[$key] = $row[“last”]; }

array_multisort($atoz, SORT_ASC, $members_list_array);

?>

The ‘magic’ happens in the last line of the code, from here you can manipulate your array data in anyway you need – for myself I had to output the content into four rows.

Displaying BuddyPress members, alphabetically by surname

Hope this helps any other BuddyPress users.

Advertisements
  1. Hi, thank you very much. Can you tell me, where exactly you put the code
    <?php
    do_action( ‘bp_before_directory_members_list’ );

    ??

    Because I always get errors and I am not used to php. Thanks again a lot!

    • Hi, you should have a ‘members’ folder in your template directory, and in there is the ‘members-loop.php’ file. You ideally want the code before the ‘<div id=”members_list>”‘.

      Hope that helps.

  2. Hm. Thanks! For me it is not working, the members are still sorted by their first name.

    The only thing I changed: I put away the
    ?>
    before endwhile

    • Argh! Well spotted, that ?> shouldn’t have been there. With that altered you should be left with an array with the correctly sorted names in it. Use a php loop to echo out the names on your page. For my page I used:

      $member_cols = 4;
      $total_member_count = count($members_list_array);
      $total_members_divided_by = ceil($total_member_count/$member_cols);
      ?>

      <div id=”members_list”>

      <?

      $add = 0;
      $a = 1;

      while ($a <= $member_cols) { ?>

      <div class=”IEcol<? echo $a; ?>” >

      <? $b = 1; while (($b <= $total_members_divided_by) & ($add != $total_member_count)) { ?>
      <div class=”members_listing”>
      <a href=”<?php echo $members_list_array[$add][“link”]; ?>”><?php echo $members_list_array[$add][“full”]; ?></a>
      <?php echo $members_list_array[$add][“icon”]; ?>
      </div>
      <? $add++; $b++; } ?>

      </div>
      <? $a++; } ?>
      </div>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: