bp_is_current_component( string $component = '' )
Check to see whether the current page belongs to the specified component.
Description
This function is designed to be generous, accepting several different kinds of value for the $component parameter. It checks $component_name against:
- the component’s root_slug, which matches the page slug in $bp->pages.
- the component’s regular slug.
- the component’s id, or ‘canonical’ name.
Parameters
- $component
-
(Optional) Name of the component being checked.
Default value: ''
Return
(bool) Returns true if the component matches, or else false.
Source
File: bp-core/bp-core-template.php
function bp_is_current_component( $component = '' ) {
// Default is no match. We'll check a few places for matches.
$is_current_component = false;
// Always return false if a null value is passed to the function.
if ( empty( $component ) ) {
return false;
}
// Backward compatibility: 'xprofile' should be read as 'profile'.
if ( 'xprofile' === $component ) {
$component = 'profile';
}
$bp = buddypress();
// Only check if BuddyPress found a current_component.
if ( ! empty( $bp->current_component ) ) {
// First, check to see whether $component_name and the current
// component are a simple match.
if ( $bp->current_component == $component ) {
$is_current_component = true;
// Since the current component is based on the visible URL slug let's
// check the component being passed and see if its root_slug matches.
} elseif ( isset( $bp->{$component}->root_slug ) && $bp->{$component}->root_slug == $bp->current_component ) {
$is_current_component = true;
// Because slugs can differ from root_slugs, we should check them too.
} elseif ( isset( $bp->{$component}->slug ) && $bp->{$component}->slug == $bp->current_component ) {
$is_current_component = true;
// Next, check to see whether $component is a canonical,
// non-translatable component name. If so, we can return its
// corresponding slug from $bp->active_components.
} elseif ( $key = array_search( $component, $bp->active_components ) ) {
if ( strstr( $bp->current_component, $key ) ) {
$is_current_component = true;
}
// If we haven't found a match yet, check against the root_slugs
// created by $bp->pages, as well as the regular slugs.
} else {
foreach ( $bp->active_components as $id ) {
// If the $component parameter does not match the current_component,
// then move along, these are not the droids you are looking for.
if ( empty( $bp->{$id}->root_slug ) || $bp->{$id}->root_slug != $bp->current_component ) {
continue;
}
if ( $id == $component ) {
$is_current_component = true;
break;
}
}
}
}
/**
* Filters whether the current page belongs to the specified component.
*
* @since BuddyPress 1.5.0
*
* @param bool $is_current_component Whether or not the current page belongs to specified component.
* @param string $component Name of the component being checked.
*/
return apply_filters( 'bp_is_current_component', $is_current_component, $component );
}
Changelog
| Version | Description |
|---|---|
| BuddyPress 1.5.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.