bp_is_current_component

Check to see whether the current page belongs to the specified component.

Description

bp_is_current_component( (string) $component = '' ); 

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 (1)

0. $component — Optional. (string) => ''
Name of the component being checked.

Usage

  1. if ( !function_exists( 'bp_is_current_component' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-template.php'; 
  3.  
  4. // Name of the component being checked. 
  5. $component = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bp_is_current_component($component); 
  9.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-template.php  
  1. function bp_is_current_component( $component = '' ) { 
  2.  
  3. // Default is no match. We'll check a few places for matches. 
  4. $is_current_component = false; 
  5.  
  6. // Always return false if a null value is passed to the function. 
  7. if ( empty( $component ) ) { 
  8. return false; 
  9.  
  10. // Backward compatibility: 'xprofile' should be read as 'profile'. 
  11. if ( 'xprofile' === $component ) { 
  12. $component = 'profile'; 
  13.  
  14. $bp =buddypress); 
  15.  
  16. // Only check if BuddyPress found a current_component. 
  17. if ( ! empty( $bp->current_component ) ) { 
  18.  
  19. // First, check to see whether $component_name and the current 
  20. // component are a simple match. 
  21. if ( $bp->current_component == $component ) { 
  22. $is_current_component = true; 
  23.  
  24. // Since the current component is based on the visible URL slug let's 
  25. // check the component being passed and see if its root_slug matches. 
  26. } elseif ( isset( $bp->{$component}->root_slug ) && $bp->{$component}->root_slug == $bp->current_component ) { 
  27. $is_current_component = true; 
  28.  
  29. // Because slugs can differ from root_slugs, we should check them too. 
  30. } elseif ( isset( $bp->{$component}->slug ) && $bp->{$component}->slug == $bp->current_component ) { 
  31. $is_current_component = true; 
  32.  
  33. // Next, check to see whether $component is a canonical,  
  34. // non-translatable component name. If so, we can return its 
  35. // corresponding slug from $bp->active_components. 
  36. } elseif ( $key = array_search( $component, $bp->active_components ) ) { 
  37. if ( strstr( $bp->current_component, $key ) ) { 
  38. $is_current_component = true; 
  39.  
  40. // If we haven't found a match yet, check against the root_slugs 
  41. // created by $bp->pages, as well as the regular slugs. 
  42. } else { 
  43. foreach ( $bp->active_components as $id ) { 
  44. // If the $component parameter does not match the current_component,  
  45. // then move along, these are not the droids you are looking for. 
  46. if ( empty( $bp->{$id}->root_slug ) || $bp->{$id}->root_slug != $bp->current_component ) { 
  47. continue; 
  48.  
  49. if ( $id == $component ) { 
  50. $is_current_component = true; 
  51. break; 
  52.  
  53. /** 
  54. * Filters whether the current page belongs to the specified component. 
  55. * @since 1.5.0 
  56. * @param bool $is_current_component Whether or not the current page belongs to specified component. 
  57. * @param string $component Name of the component being checked. 
  58. */ 
  59. return apply_filters( 'bp_is_current_component', $is_current_component, $component );