How to add custom navigation in BuddyPress

To add custom navigation in BuddyPress using functions.php, you can use the bp_core_new_nav_item function in your theme’s functions.php file. This function allows you to add new navigation items to BuddyPress’ navigation menus.

Here’s an example of how you can use the bp_core_new_nav_item function to add a custom navigation item:

// Add custom navigation in BuddyPress
// More snippets at wpunplugged.com 

function my_custom_nav_setup() {
	bp_core_new_nav_item( array(
		'name'                    => 'My Custom Nav',
		'slug'                    => 'my-custom-nav',
		'position'                => 100,
		'show_for_displayed_user' => true,
		'screen_function'         => 'my_custom_nav_screen',
		'default_subnav_slug'     => 'my-custom-nav-item',
		'item_css_id'             => 'my-custom-nav',
		'user_has_access'         => bp_is_my_profile()
	) );
}
add_action( 'bp_setup_nav', 'my_custom_nav_setup', 100 );
?>

This code will add a new navigation item called “My Custom Nav” to the navigation menu, with the slug “my-custom-nav”. The navigation item will be positioned at the 100th position in the menu, and it will be displayed for the displayed user.

The screen_function parameter specifies the function that will be called when the navigation item is clicked. In this case, the function my_custom_nav_screen will be called.

The default_subnav_slug parameter specifies the default subnav item to display when the navigation item is clicked. In this case, the subnav item with the slug “my-custom-nav-item” will be displayed by default.

The item_css_id parameter specifies the CSS ID to use for the navigation item. This can be used to apply custom styling to the navigation item.

The user_has_access parameter specifies a function or boolean value that determines whether the current user has access to the navigation item. In this case, the bp_is_my_profile function is used to determine whether the user is viewing their own profile.

You can also add subnav items to your custom navigation item using the bp_core_new_subnav_item function. Here’s an example of how you can use this function:

// Add custom navigation in BuddyPress
// More snippets at wpunplugged.com 

function my_custom_nav_item_setup() {
	bp_core_new_subnav_item( array(
		'name'            => 'My Custom Nav Item',
		'slug'            => 'my-custom-nav-item',
		'parent_slug'     => 'my-custom-nav',
		'parent_url'      => bp_displayed_user_domain() . 'my-custom-nav/',
		'screen_function' => 'my_custom_nav_item_screen',
		'position'        => 10,
		'item_css_id'     => 'my-custom-nav-item'
	) );
}
add_action( 'bp_setup_nav', 'my_custom_nav_item_setup', 100 );

This code will add a new subnav item called “My Custom Nav Item” to the “My Custom Nav” navigation item, with the slug “my-custom-nav-item”. The subnav item will be positioned at the 10th position in the menu, and it will have the CSS ID “my-custom-nav-item”.

The parent_slug parameter specifies the slug of the parent navigation item, and the parent_url parameter specifies the URL of the parent navigation item.

The screen_function parameter specifies the function that will be called when the subnav item is clicked. In this case, the function my_custom_nav_item_screen will be called.

You will need to define the my_custom_nav_screen and my_custom_nav_item_screen functions in your theme’s functions.php file or in a separate plugin file. These functions should contain the code to display the content for your custom navigation items.

Share this post

You might also like...

MemberPress Membership login for WordPress Review
Content restriction

MemberPress for WordPress Review

MemberPress for WordPress is a powerful membership plugin that makes it easy to create and manage memberships on any WordPress website. It allows you to

Read More »

Comment

Your email address will not be published. Required fields are marked *