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

Register a screen function, whether or not a related subnav link exists.

Description

Parameters

$args

(Optional) Array describing the new 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.
  • '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

(null|false) Returns false on failure.

Source

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

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

	$r = wp_parse_args( $args, array(
		'slug'              => false, // URL slug for the screen.
		'parent_slug'       => false, // URL slug of the parent screen.
		'user_has_access'   => true,  // Can the user visit this screen?
		'no_access_url'     => '',
		'site_admin_only'   => false, // Can only site admins visit this screen?
		'screen_function'   => false, // The name of the function to run when clicked.
	) );

	/*
	 * Hook the screen function for the added subnav item. But this only needs to
	 * be done if this subnav item is the current view, and the user has access to the
	 * subnav item. We figure out whether we're currently viewing this subnav by
	 * checking the following two conditions:
	 *   (1) Either:
	 *       (a) the parent slug matches the current_component, or
	 *       (b) the parent slug matches the current_item
	 *   (2) And either:
	 *       (a) the current_action matches $slug, or
	 *       (b) there is no current_action (ie, this is the default subnav for the parent nav)
	 *       and this subnav item is the default for the parent item (which we check by
	 *       comparing this subnav item's screen function with the screen function of the
	 *       parent nav item in the component's primary nav). This condition only arises
	 *       when viewing a user, since groups should always have an action set.
	 */

	// If we *don't* meet condition (1), return.
	if ( ! bp_is_current_component( $r['parent_slug'] ) && ! bp_is_current_item( $r['parent_slug'] ) ) {
		return;
	}

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

	// If we *do* meet condition (2), then the added subnav item is currently being requested.
	if ( ( bp_current_action() && bp_is_current_action( $r['slug'] ) ) || ( bp_is_user() && ! bp_current_action() && ! empty( $parent_nav->screen_function ) && $r['screen_function'] == $parent_nav->screen_function ) ) {

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

		$hooked = bp_core_maybe_hook_new_subnav_screen_function( $r, $component );

		// If redirect args have been returned, perform the redirect now.
		if ( ! empty( $hooked['status'] ) && 'failure' === $hooked['status'] && isset( $hooked['redirect_args'] ) ) {
			bp_core_no_access( $hooked['redirect_args'] );
		}
	}
}

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.