Last active
July 11, 2017 13:09
-
-
Save lenasterg/26ef43b785d1fd5cb9b04e15449a6314 to your computer and use it in GitHub Desktop.
This gist works when there are already bp_member_types. It adds tab for each member type which has_directory and at least one active user of this bp_member_type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////----- | |
// 2. display members type tabs | |
function ls_display_members_directory_tabs() { | |
$member_types = bp_get_member_types( array(), 'objects' ); | |
// Loop in member types to build the tabs | |
foreach ( $member_types as $member_type ) : | |
if ($member_type->has_directory) { | |
if (ls_count_member_types( $member_type->name ) >0) { | |
?> | |
<li id="members-<?php echo esc_attr( $member_type->name ) ;?>"> | |
<a href="<?php bp_member_type_directory_permalink($member_type->name); ?>"><?php printf( '%s <span>%d</span>', $member_type->labels['name'], | |
ls_count_member_types( $member_type->name ) | |
); ?></a> | |
</li> | |
<?php | |
} | |
} | |
endforeach; | |
} | |
// group main nav bar | |
add_action( 'bp_members_directory_member_types', 'ls_display_members_directory_tabs' ); | |
//// 3. get the member type count | |
function ls_count_member_types( $member_type = '', $taxonomy = 'bp_member_type' ) { | |
global $wpdb; | |
$member_types = bp_get_member_types( array(), 'objects' ); | |
if ( empty( $member_type ) || empty( $member_types[ $member_type ] ) ) { | |
return false; | |
} | |
$count_types = wp_cache_get( 'bp_count_member_types', 'bp_count_member_types' ); | |
if ( ! $count_types ) { | |
if ( ! bp_is_root_blog() ) { | |
switch_to_blog( bp_get_root_blog_id() ); | |
} | |
$sql=array( | |
'select' => "SELECT count({$wpdb->users}.ID) as count, {$wpdb->terms}.slug FROM {$wpdb->users}", | |
'on' => " INNER JOIN {$wpdb->term_relationships} on {$wpdb->users}.ID={$wpdb->term_relationships}.object_id ". | |
" INNER JOIN {$wpdb->term_taxonomy} on {$wpdb->term_relationships}.term_taxonomy_id={$wpdb->term_taxonomy}.term_taxonomy_id ". | |
" INNER JOIN {$wpdb->terms} on {$wpdb->terms}.term_id={$wpdb->term_taxonomy}.term_id ", | |
'where1' => $wpdb->prepare( "WHERE {$wpdb->term_taxonomy}.taxonomy = %s", $taxonomy ), | |
'where2' => " AND {$wpdb->users}.user_status = 0" , | |
'group by'=> " GROUP BY {$wpdb->terms}.slug " | |
); | |
$count_types = $wpdb->get_results( join( ' ', $sql ) ); | |
wp_cache_set( 'bp_count_member_types', $count_types, 'bp_count_member_types' ); | |
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]; | |
} | |
//// 4. display member type tab content | |
function ls_set_has_members_type_arg( $args = array() ) { | |
// Get member types to check scope | |
$member_types = bp_get_member_types( array(), 'objects' ); | |
// Set the group type arg if scope match one of the registered group 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', 'ls_set_has_members_type_arg', 10, 2 ); | |
//// 5. display member type on directory | |
function ls_member_directory_show_member_type( $member_id = NULL ) { | |
if ( empty( $member_id ) ) { | |
$member_id = bp_get_member_user_id(); | |
} | |
// Members directory. | |
if( bp_is_active( 'members' ) && bp_is_members_directory() ) { | |
// Get member types to check scope | |
$member_types = bp_get_member_type( $member_id ,false ); | |
if ($member_types) { | |
foreach ( $member_types as $type ) { | |
$obj =(array)bp_get_member_type_object( $type ); | |
// Member type name/description. | |
printf( '<div class="dir-desc-'.$obj['labels']['singular_name'].'" ><span class="dir-desc-span-name">%1$s</span>.', $obj['labels']['singular_name'].'</div>' ); | |
} | |
} | |
} | |
} | |
add_action( 'bp_directory_members_item', 'ls_member_directory_show_member_type' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment