Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code Snippet to put in your bp-custom.php file to be able to filter the BuddyPress members directory by member types (requires BuddyPress version 2.2-beta1)
<?php
/**
* Using BuddyPress Member types API
*
* see codex: https://codex.buddypress.org/developer/member-types/
*
* Required config:
* - WordPress 4.1
* - BuddyPress 2.2
*
* License: GNU/GPL 2
* Author: imath
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Register member types.
*
* From the wp-admin/extended profile
* it will be possible to set the user's member type
*/
function using_mt_register_member_types() {
bp_register_member_type( 'student', array(
'labels' => array(
'name' => __( 'Students', 'using-mt' ),
'singular_name' => __( 'Student', 'using-mt' ),
),
) );
bp_register_member_type( 'teacher', array(
'labels' => array(
'name' => __( 'Teachers', 'using-mt' ),
'singular_name' => __( 'Teacher', 'using-mt' ),
),
) );
}
add_action( 'bp_init', 'using_mt_register_member_types' );
/**
* Get the member type count
*
* @todo use a WP Taxonomy function ?
*/
function using_mt_count_member_types( $member_type = '', $taxonomy = 'bp_member_type' ) {
global $wpdb;
$member_types = bp_get_member_types();
if ( empty( $member_type ) || empty( $member_types[ $member_type ] ) ) {
return false;
}
$count_types = wp_cache_get( 'using_mt_count_member_types', 'using_mt_bp_member_type' );
if ( ! $count_types ) {
if ( ! bp_is_root_blog() ) {
switch_to_blog( bp_get_root_blog_id() );
}
$sql = array(
'select' => "SELECT t.slug, tt.count FROM {$wpdb->term_taxonomy} tt LEFT JOIN {$wpdb->terms} t",
'on' => 'ON tt.term_id = t.term_id',
'where' => $wpdb->prepare( 'WHERE tt.taxonomy = %s', $taxonomy ),
);
$count_types = $wpdb->get_results( join( ' ', $sql ) );
wp_cache_set( 'using_mt_count_member_types', $count_types, 'using_mt_bp_member_type' );
restore_current_blog();
}
$type_count = wp_filter_object_list( $count_types, array( 'slug' => $member_type ), 'and', 'count' );
$type_count = array_values( $type_count );
if ( empty( $type_count ) ) {
return 0;
}
return (int) $type_count[0];
}
/**
* Display tabs used to filter the members directory by types.
*/
function using_mt_display_directory_tabs() {
$member_types = bp_get_member_types( array(), 'objects' );
// Loop in member types to build the tabs
foreach ( $member_types as $member_type ) : ?>
<li id="members-<?php echo esc_attr( $member_type->name ) ;?>">
<a href="<?php bp_members_directory_permalink(); ?>"><?php printf( '%s <span>%d</span>', $member_type->labels['name'], using_mt_count_member_types( $member_type->name ) ); ?></a>
</li>
<?php endforeach;
}
add_action( 'bp_members_directory_member_types', 'using_mt_display_directory_tabs' );
/**
* Alter the members query arguments to filter the members query by selected type.
*/
function using_mt_set_has_members_type_arg( $args = array() ) {
// Get member types to check scope
$member_types = bp_get_member_types();
// Set the member type arg if scope match one of the registered member type
if ( ! empty( $args['scope'] ) && ! empty( $member_types[ $args['scope'] ] ) ) {
$args['member_type'] = $args['scope'];
}
return $args;
}
add_filter( 'bp_before_has_members_parse_args', 'using_mt_set_has_members_type_arg', 10, 1 );
/**
* Clean count cache when one of the member type count has been updated
*/
function using_mt_clean_count_cache( $term = 0, $taxonomy = null ) {
if ( empty( $term ) || empty( $taxonomy->name ) || 'bp_member_type' != $taxonomy->name ) {
return;
}
wp_cache_delete( 'using_mt_count_member_types', 'using_mt_bp_member_type' );
}
add_action( 'edited_term_taxonomy', 'using_mt_clean_count_cache', 10, 2 );
/**
* Display the member type of the displayed user
*/
function using_mt_member_header_display() {
$member_type = bp_get_member_type( bp_displayed_user_id() );
if ( empty( $member_type ) ) {
return;
}
$member_type_object = bp_get_member_type_object( $member_type );
?>
<p class="member_type"><?php echo esc_html( $member_type_object->labels['singular_name'] ); ?></p>
<?php
}
add_action( 'bp_before_member_header_meta', 'using_mt_member_header_display' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment