In our previous BuddyPress Profile Menu tutorial, we provided examples to change the default BP menu and subnav names and positions. In this tutorial, we cover how to add complete new profile pages to the BP user menu, including submenu items, and how to remove default nav items entirely.
Remove Unwanted Default Profile Menu Items
The best way to disable/remove a menu item in the BP members profile, is to disable the component from the BuddyPress Settings. This guarantees that not only would the menu items not display, but its pages would no longer exist, which could still be accessed directly via its URL.
In a scenario where you'd like to remove a menu item from the BuddyPress profile, but would like to keep the component enabled, e.g. Forums, Site Tracking, then the safest way is to use the unset
function:
unset($bp->bp_nav['blogs']);
Simply apply this function to the bp_setup_nav
action, as explained in the previous BuddyPress tutorial (we will show a complete function below).
Adding New Profile Nav Items
To add completely new items to the profile navigation, we will use the bp_core_new_nav_item
function. The function has multiple parameters, which are described here.
Let's use this code to add a new profile menu item that we will use to display a users posts:
add_action('bp_setup_nav', 'mb_bp_profile_menu_posts', 301 );
function mb_bp_profile_menu_posts() {
global $bp;
bp_core_new_nav_item(
array(
'name' => 'My Posts',
'slug' => 'posts',
'position' => 11,
'default_subnav_slug' => 'published', // We add this submenu item below
'screen_function' => 'mb_author_posts'
)
);
}
The code will add a menu item right after the first default menu item (after the Activity tab) and it will attempt to display the content from the mb_author_posts
function.
Assigning New Subnav Items
To attach a subnavigation to our new menu item, we will use the function bp_core_new_subnav_item
and add it to the action bp_setup_nav
after the function to create the parent menu tab has run:
add_action('bp_setup_nav', 'mb_bp_profile_submenu_posts', 302 );
function mb_bp_profile_submenu_posts() {
global $bp;
if(!is_user_logged_in()) return '';
bp_core_new_subnav_item( array(
'name' => 'Published',
'slug' => 'published',
'parent_url' => $bp->loggedin_user->domain . $bp->bp_nav['posts']['slug'] . '/'; ,
'parent_slug' => $bp->bp_nav['posts']['slug'],
'position' => 10,
'screen_function' => 'mb_author_posts'
)
);
bp_core_new_subnav_item( array(
'name' => 'Drafts',
'slug' => 'drafts',
'parent_url' => $bp->loggedin_user->domain . $bp->bp_nav['posts']['slug'] . '/'; ,
'parent_slug' => $bp->bp_nav['posts']['slug'],
'position' => 20,
'screen_function' => 'mb_author_posts_drafts'
)
);
}
Adding Profile Menu Tab Content
Now that we have our navigation items in place, we need to attach the functions that displays the content. We do this by attaching our functions content output to the BuddyPress template /members/single/plugins
:
Here an example of how our function could look like:
function mb_author_posts(){
add_action( 'bp_template_content', 'mb_show_posts' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function mb_show_posts() {
echo 'Function to Generate the displayed users posts.';
// echo do_shortcode('[mb_feedburner count=5 user="MaximusBusiness"]'); // Displays a shortcode output.
}
The shortcode example above has been taken from the tutorial Must Have WordPress Shortcodes.
You would duplicate the above structure for the second subnavigation item if applicable.
Feel free to comment or request more examples/explanations for any of the above.
Said on Sep 22, 2013 by Anson -
Said on Oct 10, 2013 by Gunn4r -
Said on Mar 19, 2014 by Charlie -
Said on Apr 26, 2014 by Matilda H. -
Said on Jun 15, 2014 by Patrik -
Said on Aug 15, 2014 by Saskia -
Said on Sep 19, 2014 by asif -
Said on Nov 27, 2014 by Vikas -
Said on Nov 27, 2014 by Vikas -
Said on Mar 28, 2015 by Michael -