How can I create an auto-populated menu that is automatically assigned to a location?

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?

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 );
        }

    } 
}

2 s
2

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( "https://wordpress.stackexchange.com/" ), 
    '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.

Leave a Comment