I’m trying to add BuddyPress nav menu support to my theme and, unfortunately, BP’s template tags still aren’t fully up to snuff. (Basically, if you’re not making an explicit child theme for the BP Default theme, you’ve got to reinvent several wheels.)
So what I’d like to do is
- Detect when BP is active (I know how to do this)
- Register a menu position when BP is active (also a known quantity)
- Create a default menu containing links to the BP sections (this is where the hole in my knowledge exists)
- Assign said default menu to the newly-registered position
So, essentially, with my theme active, if a user activates BuddyPress, they’ll automatically get a menu with Members, Forums, Activity, etc. and it will be displayed to a position, but if users wanted to override the menu, they would be free to do so.
Thoughts?
EDIT 1
Bainternet wins the prize. Here’s what I did, slightly modified from his solution:
I conditionally registered a menu location
if( function_exists( 'bp_get_loggedin_user_nav' ) ){
register_nav_menu( 'lblgbpmenu', 'Default BuddyPress Menu' );
}
I then conditionally hooked in a call to the menu setup
if( function_exists( 'bp_get_loggedin_user_nav' ) ){
add_action( 'widgets_init', 'lblg_add_default_buddypress_menu' );
}
Then, last of all, I actually registered the menu
function lblg_add_default_buddypress_menu(){
global $lblg_themename;
$menuname = $lblg_themename . ' BuddyPress Menu';
$bpmenulocation = 'lblgbpmenu';
// Does the menu exist already?
$menu_exists = wp_get_nav_menu_object( $menuname );
// If it doesn't exist, let's create it.
if( !$menu_exists){
$menu_id = wp_create_nav_menu($menuname);
// Set up default BuddyPress links and add them to the menu.
wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => __('Home'),
'menu-item-classes' => 'home',
'menu-item-url' => home_url( "https://wordpress.stackexchange.com/" ),
'menu-item-status' => 'publish'));
wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => __('Activity'),
'menu-item-classes' => 'activity',
'menu-item-url' => home_url( '/activity/' ),
'menu-item-status' => 'publish'));
wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => __('Members'),
'menu-item-classes' => 'members',
'menu-item-url' => home_url( '/members/' ),
'menu-item-status' => 'publish'));
wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => __('Groups'),
'menu-item-classes' => 'groups',
'menu-item-url' => home_url( '/groups/' ),
'menu-item-status' => 'publish'));
wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => __('Forums'),
'menu-item-classes' => 'forums',
'menu-item-url' => home_url( '/forums/' ),
'menu-item-status' => 'publish'));
// Grab the theme locations and assign our newly-created menu
// to the BuddyPress menu location.
if( !has_nav_menu( $bpmenulocation ) ){
$locations = get_theme_mod('nav_menu_locations');
$locations[$bpmenulocation] = $menu_id;
set_theme_mod( 'nav_menu_locations', $locations );
}
}
}