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

Add a link to the main BuddyPress navigation.


Description Description


Parameters Parameters

$args

(Optional) Array describing the new nav item.

  • 'component_id'
    (string) Optional. The ID of the component registering this nav item. Defaults to the the value of $slug.
  • 'name'
    (string) Display name for the nav item.
  • 'slug'
    (string) Unique URL slug for the nav item.
  • 'item_css_id'
    (bool|string) Optional. 'id' attribute for the nav item. Default: the value of $slug.
  • 'show_for_displayed_user'
    (bool) Optional. Whether the nav item should be visible when viewing a member profile other than your own. 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 nav array. Default: 99.
  • 'screen_function'
    (callable) The callback function that will run when the nav item is clicked.
  • 'default_subnav_slug'
    (bool|string) Optional. The slug of the default subnav item to select when the nav item is clicked.

Default value: ''

$component

(Optional) Component that the nav belongs to.

Default value: 'members'


Top ↑

Return Return

(false|array) Returns false on failure, new nav item on success.


Top ↑

Source Source

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

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

	$defaults = array(
		'component_id'            => '',    // The component ID registering this nav item.
		'name'                    => false, // Display name for the nav item.
		'slug'                    => false, // URL slug for the nav item.
		'item_css_id'             => false, // The CSS ID to apply to the HTML of the nav item.
		'show_for_displayed_user' => true,  // When viewing another user does this nav item show up?
		'site_admin_only'         => false, // Can only site admins see this nav item?
		'position'                => 99,    // Index of where this nav item should be positioned.
		'screen_function'         => false, // The name of the function to run when clicked.
		'default_subnav_slug'     => false  // The slug of the default subnav item to select when clicked.
	);

	$r = wp_parse_args( $args, $defaults );

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

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

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

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

	$nav_item = array(
		'component_id'            => $r['component_id'],
		'name'                    => $r['name'],
		'slug'                    => $r['slug'],
		'link'                    => trailingslashit( bp_loggedin_user_domain() . $r['slug'] ),
		'css_id'                  => $r['item_css_id'],
		'show_for_displayed_user' => $r['show_for_displayed_user'],
		'position'                => $r['position'],
		'screen_function'         => &$r['screen_function'],
		'default_subnav_slug'	  => $r['default_subnav_slug']
	);

	// Add the item to the nav.
	buddypress()->{$component}->nav->add_nav( $nav_item );

	/**
	 * Fires after a link is added to the main BuddyPress nav.
	 *
	 * @since 2.4.0
	 * @since 2.6.0 Added `$component` parameter.
	 *
	 * @param array  $r         Parsed arguments for the nav item.
	 * @param array  $args      Originally passed in arguments for the nav item.
	 * @param array  $defaults  Default arguments for a nav item.
	 * @param string $component Component that the nav belongs to.
	 */
	do_action( 'bp_core_create_nav_link', $r, $args, $defaults, $component );

	return $nav_item;
}

Top ↑

Changelog Changelog

Changelog
Version Description
4.0.0 Introduced $component_id argument.
2.6.0 Introduced the $component parameter. Began returning a BP_Core_Nav_Item object on success.
2.4.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.