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

1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
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

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.