WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

  1. Detect when BP is active (I know how to do this)
  2. Register a menu position when BP is active (also a known quantity)
  3. Create a default menu containing links to the BP sections (this is where the hole in my knowledge exists)
  4. 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?


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( '/' ), 
            '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 );

share|improve this question
See also wordpress.stackexchange.com/a/73964 where I mention a bug. – OzzyCzech Nov 26 '12 at 11:41
up vote 5 down vote accepted

So basically you are asking how to create a custom menu by code and assign it to a menu location:

 //give your menu a name
$name = 'theme default menu';
 //create the menu
$menu_id = wp_create_nav_menu($name);
 //then get the menu object by its name
$menu = get_term_by( 'name', $name, 'nav_menu' );
 //then add the actuall link/ menu item and you do this for each item you want to add
wp_update_nav_menu_item($menu->term_id, 0, array(
    'menu-item-title' =>  __('Home'),
    'menu-item-classes' => 'home',
    'menu-item-url' => home_url( '/' ), 
    'menu-item-status' => 'publish'));
// you add as many items ass you need with wp_update_nav_menu_item()

//then you set the wanted theme  location
$locations = get_theme_mod('nav_menu_locations');
$locations['LOCATION_NAME'] = $menu->term_id;
set_theme_mod( 'nav_menu_locations', $locations );

So all you have to do is add as many links as you want, change LOCATION_NAME to the actual location name and make sure this code is only run once.

share|improve this answer
This, I think, might be along the lines of what I'm looking for. – ZaMoose Apr 26 '11 at 19:47
This worked out, with modifications. See above, where I've added the solution I ended up using. Thanks! – ZaMoose May 13 '11 at 13:03

Your call to wp_nav_menu() needs to include a custom callback function, such as a call to wp_list_pages(), to which you can pass an include argument, into which you can include explicit Page IDs.

share|improve this answer
Although if BuddyPress "sections" don't correspond to WP Pages, then my solution won't work. :) – Chip Bennett Apr 26 '11 at 17:23
and it's not really answering the question in hand. – Bainternet Apr 26 '11 at 17:34
How does it not answer the question at hand? ZaMoose is asking for a custom-populated menu under specific conditions. – Chip Bennett Apr 26 '11 at 17:35
he is asking how to create a menu, add links to it and set its location. or maybe i got the question wrong. – Bainternet Apr 26 '11 at 17:41
Right - and I'm proposing a solution that precludes the need to create a custom menu. The end result is that the correct menu items display in the correct menu locations, under the specific conditions. – Chip Bennett Apr 26 '11 at 17:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.