Skip to:
Content
Pages
Categories
Search
Top
Bottom
Codex Home → Add Custom Tab to Groups Directory

Add Custom Tab to Groups Directory

This is an example of how to add a custom tab to the Groups Directory.
And call a custom template when that tab is clicked.

The same approach can be used to add a custom tab to the Members Directory.

The tab will be called Custom and it will load a template called groups-custom.php.

// add Custom tab on Groups Page
function asdf_groups_custom_tab() {

	if ( bp_is_current_action( 'custom' ) ) 
		return;

	$button_args = array(
		'id'         => 'groups-custom',
		'component'  => 'groups',
		'link_text'  => __( 'Custom', 'buddypress' ),
		'link_title' => __( 'Custom', 'buddypress' ),
		'link_class' => 'group-custom no-ajax',
		'link_href'  => trailingslashit( bp_get_groups_directory_permalink() . 'custom' ),
		'wrapper'    => false,
		'block_self' => false,
	);	
	
	?>
	<li><?php echo bp_get_button( apply_filters( 'bp_get_group_custom_button', $button_args ) ); ?></a></li>
	<?php
}
add_action( 'bp_groups_directory_group_filter', 'asdf_groups_custom_tab' );


// load the Groups Custom template
function asdf_groups_show_custom() {
	
	if ( !bp_is_groups_component() || !bp_is_current_action( 'custom' ) ) {
		return false;
	}
	
	new BP_Groups_Theme_Compat_Custom();  // see class below
	
	bp_core_load_template( 'groups/groups-custom' );

}		
add_action( 'bp_actions', 'asdf_groups_show_custom' );


// enable the loading of the custom template
class BP_Groups_Theme_Compat_Custom {

	public function __construct() {
		add_action( 'bp_setup_theme_compat', array( $this, 'is_group' ) );
	}

	public function is_group() {

		if ( ! bp_is_groups_component() )
			return;

		if ( bp_is_current_action( 'custom' ) ) {

			add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'create_dummy_post' ) );
			add_filter( 'bp_replace_the_content',                    array( $this, 'create_content'    ) );

		}
	}

	public function create_dummy_post() {

		bp_theme_compat_reset_post( array(
			'ID'             => 0,
			'post_title'     => 'Groups Custom',
			'post_author'    => 0,
			'post_date'      => 0,
			'post_content'   => '',
			'post_type'      => 'page',
			'post_status'    => 'publish',
			'is_page'        => true,
			'comment_status' => 'closed'
		) );
	}

	public function create_content() {
		return bp_buffer_template_part( 'groups/groups-custom', null, false );
	}

}

This example assumes that the groups-custom.php template is in your theme at this location:
.../wp-content/themes/your-theme/buddypress/groups/groups-custom.php

If you want to load the template from a plugin, you need to register the location of the template:

// add path to plugin templates
function asdf_register_template_location() {
    return dirname( __FILE__ ) . '/templates/';
}


function asdf_template_start() {

    if( function_exists( 'bp_register_template_stack' ) )
        bp_register_template_stack( 'asdf_register_template_location' );

}
add_action( 'bp_init', 'asdf_template_start' );

More info about using BP Theme Compat in plugins.

Skip to toolbar