BuddyPress Member Directory: Show Users by Role/ S2Member Level 

Posted by on Mar 5, 2012 in Resources, WordPress | 7 comments

The Problem

We recently had a software development project requirement where we needed to show certain BuddyPress members in the BuddyPress Members Directory. Specifically, BuddyPress members who belonged to certain roles/levels needed to be displayed. For example you may only want to show Administrators or Editors in the members directory or in our case, show only S2Members of a certain membership level.

S2Member is a WordPress membership plugin that works well with BuddyPress. It’s really more than a plugin, it actually more accurately resembles a complete membership framework that works with WordPress similar in scope to BuddyPress.  S2Member comes in two flavors, Free & Pro.  The free version is amazingly robust and should serve the needs of most basic WordPress / BuddyPress site requirements, however the Pro version offers a full suite of functionality for more complex installations.

The Solution

In our case, we needed to show all WordPress Administrators and S2Members with a membership level of 1 in the directory.  The BuddyPress members directory is generated through The Members Loop contained in the directory: plugins/buddypress/bp-themes/bp-default/members/members-loop.php.  Members-loop.php is where you’ll modify the code to show only certain types of BuddyPress users instead of the default which shows all users.

The code itself is fairly straightforward.  In the while loop that generates each BuddyPress user listing in the directory, you’ll need to get each member’s user id and then get their role. After

<?php while ( bp_members() ) : bp_the_member(); ?>

add

<?php $user=bp_get_member_user_id();

This sets the variable $user to the current BuddyPress user being fetched in the While loop.

Next, add

$s2member_var = get_user_field ("s2member_access_role", $user);

You can use any variable name here, in our case because we primarily want to show S2Members of level 1, we named our var, $s2member_var. This line gets the user field “s2member_access_role” for the current user in the loop who’s user id = $user.

Lastly, we want to only show BuddyPress users in the directory who are Administrators or S2members of level 1. Using a simple ‘if’ statement, we say if $s2member_var which contains the current user’s role (Administrator, Editor, Subscriber, s2member_level0, s2member_level1, s2member_level2, etc.) is exactly equal to “s2member_level1″ or “administrator” then show the member in the BuddyPress members directory. If not, skip this user and loop to the next one.

Here’s the code to begin the loop:

<?php while ( bp_members() ) : bp_the_member(); ?>
 
<?php $user=bp_get_member_user_id();
 
$s2member_var = get_user_field ("s2member_access_role", $user);
 
if ($s2member_var == "s2member_level1" || $s2member_var == "administrator") { ?>
 
	<li>	
			<div class="item-avatar">
				<a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar(); ?></a>
			</div>

Here’s the entire members-loop.php file for reference. Please note other customizations were done to the loop, including:

1) Listing users in alphabetical order in the BuddyPress members directory
2) Showing BuddyPress custom profile fields in the BuddyPress members directory from the BuddyPress registration form.
3) Removing the “last active” label from each user’s BuddyPress members directory listing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
 
/**
 * BuddyPress - Members Loop
 *
 * Querystring is set via AJAX in _inc/ajax.php - bp_dtheme_object_filter()
 *
 * @package BuddyPress
 * @subpackage bp-default
 */
 
?>
 
<?php do_action( 'bp_before_members_loop' ); ?>
 
<?php if ( bp_has_members( bp_ajax_querystring( 'members' ).'&type=alphabetical' ) ) : ?>
 
	<div id="pag-top" class="pagination">
 
		<div class="pag-count" id="member-dir-count-top">
 
			<?php bp_members_pagination_count(); ?>
 
		</div>
 
		<div class="pagination-links" id="member-dir-pag-top">
 
			<?php bp_members_pagination_links(); ?>
 
		</div>
 
	</div>
 
	<?php do_action( 'bp_before_directory_members_list' ); ?>
 
	<ul id="members-list" class="item-list" role="main">
 
	<?php while ( bp_members() ) : bp_the_member(); ?>
 
<?php $user=bp_get_member_user_id();
 
$s2member_var = get_user_field ("s2member_access_role", $user);
 
if ($s2member_var == "s2member_level1" || $s2member_var == "administrator") {
 
?>
 
 
		<li>
 
			<div class="item-avatar">
				<a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar(); ?></a>
			</div>
 
			<div class="item" style="float:left; width:80%;">
				<div class="item-title">
					<a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>
 
					<?php if ( bp_get_member_latest_update() ) : ?>
 
						<span class="update"> <?php bp_member_latest_update(); ?></span>
 
					<?php endif; ?>
 
				</div>
 
 
 
				<?php do_action( 'bp_directory_members_item' ); ?>
 
				<?php
				  bp_member_profile_data( 'field=Company' );
				  ?><br/><?php
                              bp_member_profile_data( 'field=Phone' );
                              ?><br/><?php
			         bp_member_profile_data( 'field=Email' );
 
 
				?>
			</div>
 
			<div class="action">
 
				<?php do_action( 'bp_directory_members_actions' ); ?>
 
			</div>
 
			<div class="clear"></div>
		</li>
<?php } ?>
	<?php endwhile; ?>
 
 
	</ul>
 
	<?php do_action( 'bp_after_directory_members_list' ); ?>
 
	<?php bp_member_hidden_fields(); ?>
 
	<div id="pag-bottom" class="pagination">
 
		<div class="pag-count" id="member-dir-count-bottom">
 
			<?php bp_members_pagination_count(); ?>
 
		</div>
 
		<div class="pagination-links" id="member-dir-pag-bottom">
 
			<?php bp_members_pagination_links(); ?>
 
		</div>
 
	</div>
 
<?php else: ?>
 
	<div id="message" class="info">
		<p><?php _e( "Sorry, no members were found.", 'buddypress' ); ?></p>
	</div>
 
<?php endif; ?>
 
<?php do_action( 'bp_after_members_loop' ); ?>

7 Responses to “BuddyPress Member Directory: Show Users by Role/ S2Member Level”

  1. sascha says:

    Hi there,
    does this solution show the right amount of users/members in the tab and does it update when you sort by last active/recent registered/alphabetical via the AJAX?

  2. Thanks! Hard to find easy small snippets for s2member.

Leave a Reply

*