bp_core_create_subnav_link( array|string $args = '', string $component = 'members' )

Add a subnav link to the BuddyPress navigation.

Description

Parameters

$args

(Optional) Array describing the new subnav item.

  • 'name'
    (string) Display name for the subnav item.
  • 'slug'
    (string) Unique URL slug for the subnav item.
  • 'parent_slug'
    (string) Slug of the top-level nav item under which the new subnav item should be added.
  • 'parent_url'
    (string) URL of the parent nav item.
  • 'item_css_id'
    (bool|string) Optional. 'id' attribute for the nav item. Default: the value of $slug.
  • 'user_has_access'
    (bool) Optional. True if the logged-in user has access to the subnav item, otherwise false. Can be set dynamically when registering the subnav; eg, use bp_is_my_profile() to restrict access to profile owners only. Default: true.
  • 'site_admin_only'
    (bool) Optional. Whether the nav item should be visible only to site admins (those with the 'bp_moderate' cap). Default: false.
  • 'position'
    (int) Optional. Numerical index specifying where the item should appear in the subnav array. Default: 90.
  • 'screen_function'
    (callable) The callback function that will run when the nav item is clicked.
  • 'link'
    (string) Optional. The URL that the subnav item should point to. Defaults to a value generated from the $parent_url + $slug.
  • 'show_in_admin_bar'
    (bool) Optional. Whether the nav item should be added into the group's "Edit" Admin Bar menu for group admins. Default: false.

Default value: ''

$component

(Optional) The component the navigation is attached to. Defaults to 'members'.

Default value: 'members'

Return

(false|array) Returns false on failure, new BP_Core_Nav_Item instance on success.

Source

File: bp-core/bp-core-buddybar.php

function bp_core_create_subnav_link( $args = '', $component = 'members' ) {
	$bp = buddypress();

	$r = wp_parse_args( $args, array(
		'name'              => false, // Display name for the nav item.
		'slug'              => false, // URL slug for the nav item.
		'parent_slug'       => false, // URL slug of the parent nav item.
		'parent_url'        => false, // URL of the parent item.
		'item_css_id'       => false, // The CSS ID to apply to the HTML of the nav item.
		'user_has_access'   => true,  // Can the logged in user see this nav item?
		'no_access_url'     => '',
		'site_admin_only'   => false, // Can only site admins see this nav item?
		'position'          => 90,    // Index of where this nav item should be positioned.
		'screen_function'   => false, // The name of the function to run when clicked.
		'link'              => '',    // The link for the subnav item; optional, not usually required.
		'show_in_admin_bar' => false, // Show the Manage link in the current group's "Edit" Admin Bar menu.
	) );

	// If we don't have the required info we need, don't create this subnav item.
	if ( empty( $r['name'] ) || empty( $r['slug'] ) || empty( $r['parent_slug'] ) || empty( $r['parent_url'] ) || empty( $r['screen_function'] ) )
		return false;

	// Link was not forced, so create one.
	if ( empty( $r['link'] ) ) {
		$r['link'] = trailingslashit( $r['parent_url'] . $r['slug'] );

		$parent_nav = $bp->{$component}->nav->get_primary( array( 'slug' => $r['parent_slug'] ), false );

		// If this sub item is the default for its parent, skip the slug.
		if ( $parent_nav ) {
			$parent_nav_item = reset( $parent_nav );
			if ( ! empty( $parent_nav_item->default_subnav_slug ) && $r['slug'] === $parent_nav_item->default_subnav_slug ) {
				$r['link'] = trailingslashit( $r['parent_url'] );
			}
		}
	}

	// If this is for site admins only and the user is not one, don't create the subnav item.
	if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
		return false;
	}

	if ( empty( $r['item_css_id'] ) ) {
		$r['item_css_id'] = $r['slug'];
	}

	$subnav_item = array(
		'name'              => $r['name'],
		'link'              => $r['link'],
		'slug'              => $r['slug'],
		'parent_slug'       => $r['parent_slug'],
		'css_id'            => $r['item_css_id'],
		'position'          => $r['position'],
		'user_has_access'   => $r['user_has_access'],
		'no_access_url'     => $r['no_access_url'],
		'screen_function'   => &$r['screen_function'],
		'show_in_admin_bar' => (bool) $r['show_in_admin_bar'],
	);

	buddypress()->{$component}->nav->add_nav( $subnav_item );

	return $subnav_item;
}

Changelog

Changelog
Version Description
BuddyPress 2.4.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.