bp_core_new_nav_default( array|string $args = '' )

Modify the default subnav item that loads when a top level nav item is clicked.


Description Description


Parameters Parameters

$args

(Optional)

  • 'parent_slug'
    (string) The slug of the nav item whose default is being changed.
  • 'screen_function'
    (callable) The new default callback function that will run when the nav item is clicked.
  • 'subnav_slug'
    (string) The slug of the new default subnav item.

Default value: ''


Top ↑

Source Source

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

function bp_core_new_nav_default( $args = '' ) {
	$bp = buddypress();

	$defaults = array(
		'parent_slug'     => false, // Slug of the parent.
		'screen_function' => false, // The name of the function to run when clicked.
		'subnav_slug'     => false  // The slug of the subnav item to select when clicked.
	);

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

	// This is specific to Members - it's not available in Groups.
	$parent_nav = $bp->members->nav->get_primary( array( 'slug' => $r['parent_slug'] ), false );

	if ( ! $parent_nav ) {
		return ;
	}

	$parent_nav = reset( $parent_nav );

	if ( ! empty( $parent_nav->screen_function ) ) {
		// Remove our screen hook if screen function is callable.
		if ( is_callable( $parent_nav->screen_function ) ) {
			remove_action( 'bp_screens', $parent_nav->screen_function, 3 );
		}
	}

	// Edit the screen function for the parent nav.
	$bp->members->nav->edit_nav( array(
		'screen_function'     => &$r['screen_function'],
		'default_subnav_slug' => $r['subnav_slug'],
	), $parent_nav->slug );

	if ( bp_is_current_component( $parent_nav->slug ) ) {

		// The only way to tell whether to set the subnav is to peek at the unfiltered_uri
		// Find the component.
		$component_uri_key = array_search( $parent_nav->slug, $bp->unfiltered_uri );

		if ( false !== $component_uri_key ) {
			if ( ! empty( $bp->unfiltered_uri[$component_uri_key + 1] ) ) {
				$unfiltered_action = $bp->unfiltered_uri[$component_uri_key + 1];
			}
		}

		// No subnav item has been requested in the URL, so set a new nav default.
		if ( empty( $unfiltered_action ) ) {
			if ( ! bp_is_current_action( $r['subnav_slug'] ) ) {
				/*
				 * If the screen function isn't available, attempt to find it.
				 *
				 * This is due to our conditional-loading code since v3.0.0.
				 */
				if ( ! is_callable( $r['screen_function'] ) && ! empty( $parent_nav->component_id ) ) {
					$file      = $bp->core->path . 'bp-' . $parent_nav->component_id . '/screens/' . $r['subnav_slug'] . '.php';
					$file_path = realpath( $file );

					// Found the file, so require it.
					if ( $file === $file_path && file_exists( $file ) ) {
						require_once $file;
					}
				}

				if ( is_callable( $r['screen_function'] ) ) {
					add_action( 'bp_screens', $r['screen_function'], 3 );
				}

				$bp->current_action = $r['subnav_slug'];
				unset( $bp->canonical_stack['action'] );
			}

		// The URL is explicitly requesting the new subnav item, but should be
		// directed to the canonical URL.
		} elseif ( $unfiltered_action == $r['subnav_slug'] ) {
			unset( $bp->canonical_stack['action'] );

		// In all other cases (including the case where the original subnav item
		// is explicitly called in the URL), the canonical URL will contain the
		// subnav slug.
		} else {
			$bp->canonical_stack['action'] = bp_current_action();
		}
	}

	return;
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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