BuddyPress Custom Group Menu and Submenu Setup

This guide explains how to create a custom primary menu and submenus for a BuddyPress group using BuddyPress navigation APIs. It also includes a solution for handling deprecated functions from BuddyPress 12.0.0+.

Overview

We will:

  • Create a primary custom menu in the group navigation.
  • Add two submenus under this custom menu.
  • Ensure backward compatibility with BuddyPress versions before and after 12.0.0, resolving deprecated warnings.

Prerequisites

  • BuddyPress Plugin (Version 12.0.0 or older versions).

Code Snippet

Here is the complete code for adding a custom group menu and two submenus in BuddyPress.

Step 1: Adding the Primary Menu and Submenus

The following code registers a primary group menu and two submenus:

function my_custom_group_menu() {
    if ( bp_is_group() ) {
        global $bp;

        // Check which function to use for group URL based on BuddyPress version
        $group_url_function = function_exists( 'bp_get_group_url' ) ? 'bp_get_group_url' : 'bp_get_group_permalink';
        $group_url = call_user_func( $group_url_function, groups_get_current_group() );

        // Create the primary custom menu item in the group navigation
        bp_core_new_subnav_item( array(
            'name'            => __( 'Group Custom Menu', 'textdomain' ),
            'slug'            => 'group-custom-menu',
            'parent_slug'     => $bp->groups->current_group->slug, // Set the group slug as the parent slug
            'parent_url'      => trailingslashit( $group_url ), // Parent group URL
            'screen_function' => 'my_custom_group_menu_content', // Content function for the primary menu
            'position'        => 40,
            'user_has_access' => bp_group_is_member() || bp_current_user_can( 'bp_moderate' ), // Show for group members or admins
            'item_css_id'     => 'group-custom-menu',
        ));

        // Add Submenu 1 under "Group Custom Menu"
        bp_core_new_subnav_item( array(
            'name'            => __( 'Group Submenu 1', 'textdomain' ),
            'slug'            => 'group-submenu-1',
            'parent_slug'     => 'group-custom-menu', // Set 'group-custom-menu' as the parent slug
            'parent_url'      => trailingslashit( $group_url . 'group-custom-menu' ), // URL for submenu 1
            'screen_function' => 'my_custom_group_submenu_1_content', // Unique content function for submenu 1
            'position'        => 10,
            'user_has_access' => bp_group_is_member() || bp_current_user_can( 'bp_moderate' ), // Show for group members or admins
        ));

        // Add Submenu 2 under "Group Custom Menu"
        bp_core_new_subnav_item( array(
            'name'            => __( 'Group Submenu 2', 'textdomain' ),
            'slug'            => 'group-submenu-2',
            'parent_slug'     => 'group-custom-menu', // Set 'group-custom-menu' as the parent slug
            'parent_url'      => trailingslashit( $group_url . 'group-custom-menu' ), // URL for submenu 2
            'screen_function' => 'my_custom_group_submenu_2_content', // Unique content function for submenu 2
            'position'        => 20,
            'user_has_access' => bp_group_is_member() || bp_current_user_can( 'bp_moderate' ), // Show for group members or admins
        ));
    }
}
add_action( 'bp_setup_nav', 'my_custom_group_menu', 100 );

Step 2: Creating Screen Functions

You need to create separate screen functions for the primary menu and each submenu to display their content.

Primary Menu Screen Function

// Screen function for the primary group menu
function my_custom_group_menu_content() {
    add_action( 'bp_template_content', 'my_custom_group_menu_page_content' );
    bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'groups/single/plugins' ) );
}

// Content for the primary group custom menu
function my_custom_group_menu_page_content() {
    // Determine which group URL function to use for backward compatibility
    $group_url_function = function_exists( 'bp_get_group_url' ) ? 'bp_get_group_url' : 'bp_get_group_permalink';
    $group_url = call_user_func( $group_url_function, groups_get_current_group() );

    // Display primary content with static links to submenus
    echo '

' . __( 'Welcome to Group Custom Menu', 'textdomain' ) . '

'; echo '

' . __( 'This is the custom content for the group custom menu.', 'textdomain' ) . '

'; // HTML for submenus inside primary menu content echo '
'; echo '

' . __( 'Submenus', 'textdomain' ) . '

'; echo ''; echo '
'; }

Submenu 1 Screen Function

// Screen function for Group Submenu 1
function my_custom_group_submenu_1_content() {
    add_action( 'bp_template_content', 'my_custom_group_submenu_1_page_content' );
    bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'groups/single/plugins' ) );
}

// Content for Group Submenu 1
function my_custom_group_submenu_1_page_content() {
    echo '

' . __( 'Welcome to Group Submenu 1', 'textdomain' ) . '

'; echo '

' . __( 'This is the content for Group Submenu 1.', 'textdomain' ) . '

'; }

Submenu 2 Screen Function

// Screen function for Group Submenu 2
function my_custom_group_submenu_2_content() {
    add_action( 'bp_template_content', 'my_custom_group_submenu_2_page_content' );
    bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'groups/single/plugins' ) );
}

// Content for Group Submenu 2
function my_custom_group_submenu_2_page_content() {
    echo '

' . __( 'Welcome to Group Submenu 2', 'textdomain' ) . '

'; echo '

' . __( 'This is the content for Group Submenu 2.', 'textdomain' ) . '

'; }

Step 3: Fixing Deprecated Function Warning

BuddyPress 12.0.0 introduced bp_get_group_url() to replace bp_get_group_permalink(). To maintain backward compatibility and avoid warnings, use the following code:

// Determine which group URL function to use for backward compatibility
$group_url_function = function_exists( 'bp_get_group_url' ) ? 'bp_get_group_url' : 'bp_get_group_permalink';
$group_url = call_user_func( $group_url_function, groups_get_current_group() );

This will ensure that no deprecated warnings appear in BuddyPress 12.0.0+.

Conclusion

With the above code, you can create custom group menus and submenus for BuddyPress. This method handles backward compatibility with both BuddyPress 12.0.0 and earlier versions. Submenus will be displayed under the primary custom group menu with separate screen functions for each.

Last updated: April 15, 2025