Skip to content

Instantly share code, notes, and snippets.

@boonebgorges
Created February 11, 2013 13:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save boonebgorges/4754549 to your computer and use it in GitHub Desktop.
Save boonebgorges/4754549 to your computer and use it in GitHub Desktop.
Dynamically add items to a wp_nav_menu list
<?php
function bbg_activity_subnav( $items, $menu, $args ) {
// Find the Activity item
$bp_pages = bp_core_get_directory_page_ids();
if ( isset( $bp_pages['activity'] ) ) {
$activity_directory_page = $bp_pages['activity'];
} else {
return $items;
}
$activity_menu_item = 0;
foreach ( $items as $item ) {
if ( $activity_directory_page == $item->object_id ) {
$activity_menu_item = $item->ID;
}
}
if ( is_user_logged_in() ) {
$new_items_data = array(
array(
'url' => trailingslashit( bp_loggedin_user_domain() . bp_get_activity_slug() ),
'title' => 'Personal',
),
array(
'url' => trailingslashit( bp_loggedin_user_domain() . bp_get_activity_slug() . '/' . bp_get_groups_slug() ),
'title' => 'Groups',
),
array(
'url' => trailingslashit( bp_get_root_domain() . '/' . bp_get_activity_root_slug() ),
'title' => 'Sitewide',
),
);
$menu_order = count( $items ) + 1;
foreach ( $new_items_data as $new_item_data ) {
$new_item = new stdClass;
$new_item->menu_item_parent = $activity_menu_item;
$new_item->url = $new_item_data['url'];
$new_item->title = $new_item_data['title'];
$new_item->menu_order = $menu_order;
$items[] = $new_item;
$menu_order++;
}
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'bbg_activity_subnav', 10, 3 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment