bp_core_menu_highlight_parent_page( array $retval, WP_Post $page )

Adds current page CSS classes to the parent BP page in a WP Page Menu.

Description

Because BuddyPress primarily uses virtual pages, we need a way to highlight the BP parent page during WP menu generation. This function checks the current BP component against the current page in the WP menu to see if we should highlight the WP page.

Parameters

$retval

(array) (Required) CSS classes for the current menu page in the menu.

$page

(WP_Post) (Required) The page properties for the current menu item.

Return

(array)

Source

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

function bp_core_menu_highlight_parent_page( $retval, $page ) {
	if ( ! is_buddypress() ) {
		return $retval;
	}

	$page_id = false;

	// Loop against all BP component pages.
	foreach ( (array) buddypress()->pages as $component => $bp_page ) {
		// Handles the majority of components.
		if ( bp_is_current_component( $component ) ) {
			$page_id = (int) $bp_page->id;
		}

		// Stop if not on a user page.
		if ( ! bp_is_user() && ! empty( $page_id ) ) {
			break;
		}

		// Members component requires an explicit check due to overlapping components.
		if ( bp_is_user() && 'members' === $component ) {
			$page_id = (int) $bp_page->id;
			break;
		}
	}

	// Duplicate some logic from Walker_Page::start_el() to highlight menu items.
	if ( ! empty( $page_id ) ) {
		$_bp_page = get_post( $page_id );
		if ( isset( $page->ID ) && in_array( $page->ID, $_bp_page->ancestors, true ) ) {
			$retval[] = 'current_page_ancestor';
		}
		if ( isset( $page->ID ) && $page->ID === $page_id ) {
			$retval[] = 'current_page_item';
		} elseif ( isset( $page->ID ) && $_bp_page && $page->ID === $_bp_page->post_parent ) {
			$retval[] = 'current-menu-item';
			$retval[] = 'current_page_parent';
		}
	}

	$retval = array_unique( $retval );

	return $retval;
}

Changelog

Changelog
Version Description
BuddyPress 2.2.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.