bp_get_root_slug

Get the root slug for given component.

Description

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

The root slug is the string used when concatenating component directory URLs. For example, on an installation where the Groups component's directory is located at http://example.com/groups/, the root slug for the Groups component is groups.. This string generally corresponds to page_name of the component's directory page.

In order to maintain backward compatibility, the following procedure is used: 1) Use the short slug to get the canonical component name from the 2) Use the component name to get the root slug out of the 3) If nothing turns up, it probably means that $component is itself

Example: If your groups directory is at /community/companies, this function first uses the short slug companies (ie the current component) to look up the canonical name groups. in $bp->active_components. Then it uses groups. to get the root slug, from $bp->groups->root_slug.

Returns (string)

$root_slug The root slug.

Parameters (1)

0. $component — Optional. (string) => ''
Defaults to the current component.

Usage

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

Defined (1)

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

/bp-core/bp-core-template.php  
  1. function bp_get_root_slug( $component = '' ) { 
  2. $bp =buddypress); 
  3. $root_slug = ''; 
  4.  
  5. // Use current global component if none passed. 
  6. if ( empty( $component ) ) { 
  7. $component = bp_current_component(); 
  8.  
  9. // Component is active. 
  10. if ( ! empty( $bp->active_components[ $component ] ) ) { 
  11.  
  12. // Backward compatibility: in legacy plugins, the canonical component id 
  13. // was stored as an array value in $bp->active_components. 
  14. $component_name = ( '1' == $bp->active_components[ $component ] ) 
  15. ? $component 
  16. : $bp->active_components[$component]; 
  17.  
  18. // Component has specific root slug. 
  19. if ( ! empty( $bp->{$component_name}->root_slug ) ) { 
  20. $root_slug = $bp->{$component_name}->root_slug; 
  21.  
  22. // No specific root slug, so fall back to component slug. 
  23. if ( empty( $root_slug ) ) { 
  24. $root_slug = $component; 
  25.  
  26. /** 
  27. * Filters the root slug for given component. 
  28. * @since 1.5.0 
  29. * @param string $root_slug Root slug for given component. 
  30. * @param string $component Current component. 
  31. */ 
  32. return apply_filters( 'bp_get_root_slug', $root_slug, $component );