Last active
August 27, 2024 10:39
-
-
Save BhargavBhandari90/e11bc52b017d732ce621362a87348d04 to your computer and use it in GitHub Desktop.
Add custom tab on buddypress group page
This file contains hidden or 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
<?php | |
/** | |
* Add custom sub-tab on groups page. | |
*/ | |
function buddypress_custom_group_tab() { | |
// Avoid fatal errors when plugin is not available. | |
if ( ! function_exists( 'bp_core_new_subnav_item' ) || | |
! function_exists( 'bp_is_single_item' ) || | |
! function_exists( 'bp_is_groups_component' ) || | |
! function_exists( 'bp_get_group_permalink' ) || | |
empty( get_current_user_id() ) ) { | |
return; | |
} | |
// Check if we are on group page. | |
if ( bp_is_groups_component() && bp_is_single_item() ) { | |
global $bp; | |
// Get current group page link. | |
$group_link = bp_get_group_permalink( $bp->groups->current_group ); | |
// Tab args. | |
$tab_args = array( | |
'name' => esc_html__( 'Group Custom Tab', 'default' ), | |
'slug' => 'group-custom-tab', | |
'screen_function' => 'group_custom_tab_screen', | |
'position' => 60, | |
'parent_url' => $group_link, | |
'parent_slug' => $bp->groups->current_group->slug, | |
'default_subnav_slug' => 'group-custom-tab', | |
'item_css_id' => 'group-custom-tab', | |
); | |
// Add sub-tab. | |
bp_core_new_subnav_item( $tab_args, 'groups' ); | |
} | |
} | |
add_action( 'bp_setup_nav', 'buddypress_custom_group_tab' ); | |
/** | |
* Set template for new tab. | |
*/ | |
function group_custom_tab_screen() { | |
// Add title and content here - last is to call the members plugin.php template. | |
add_action( 'bp_template_title', 'custom_group_tab_title' ); | |
add_action( 'bp_template_content', 'custom_group_tab_content' ); | |
bp_core_load_template( 'buddypress/members/single/plugins' ); | |
} | |
/** | |
* Set title for custom tab. | |
*/ | |
function custom_group_tab_title() { | |
echo esc_html__( 'Custom Tab', 'default_content' ); | |
} | |
/** | |
* Display content of custom tab. | |
*/ | |
function custom_group_tab_content() { | |
echo sprintf( | |
/* translators: %1$s goes for content text. */ | |
'<h3>%1$s</h3>', | |
esc_html__( 'Content goes here for custom tab', 'default' ) | |
); | |
} |
That workded for me as well. Thank you so much for your contribution.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @BhargavBhandari90 Thanks for this brilliant code. Is it possible to make this tab visible for everyone (for non-members as well as for non-logged-in users), even in a private group? Thanks for your help to do this