Component slug vs root_slug in BP 1.3
One of the new features coming in BuddyPress 1.3 is the use of WP pages as top-level component directories. This makes it easier for BP site administrators to customize their site’s component URLs, even in a nested way. For instance, buddypress.org runs a single installation of BuddyPress, with forums directory associated with a WP page called Topics, which is a child of the top level page Support (thus the URL buddypress.org/support/topics), while the members directory is on a page called Members with parent page Community (thus buddypress.org/community/members/boonebgorges).
To allow this flexibility with top-level component slugs, some elements of the $bp global have been changed in ways that plugin authors must account for in order for their plugins to be fully compatible with BP 1.3. In BP 1.3, in addition to a
slug, each component with a top-level directory (and associated WP page) has a
root_slug. To see how the new
root_slug is defined, let’s look at how the forums component does it, using the example of buddypress.org and the URLs I described above.
if ( !defined( 'BP_FORUMS_SLUG' ) )
define ( 'BP_FORUMS_SLUG', bp_core_component_slug_from_root_slug( $bp->pages->forums->slug ) );
$bp->forums->slug = BP_FORUMS_SLUG;
$bp->forums->root_slug = $bp->pages->forums->slug;
The root_slug is set to
$bp->pages->forums->slug, which in the case of buddypress.org is the string
'support/topics'. The new function
$bp->forums->slug by taking everything after the final
/ in the root_slug – in this case, ‘topics’.
These changes matter for your plugin in two different ways.
- Concatenating URLs – When building URLs, make sure you are choosing correctly between the slug and the root slug. When the slug appears at the beginning of the URL (immediately after the root domain for the BP installation), use the root_slug. For example,
$group_directory_link = bp_get_root_domain() . $bp->groups->root_slug;
is the correct code for creating a URL like example.com/groups. On the other hand, you should use the slug when it will appear mid-URL (primarily in the members component). For example,
$my_groups_link = bp_loggedin_user_domain() . $bp->groups->slug;
will return the link for a user’s groups, such as example.com/members/boonebgorges/groups.
- Checking the current_component – In BP and BP-dependent plugins, it has been common to see which component you are using by comparing the value of
$bp->current_componentwith the component slug. In BP 1.3, this will not work in all cases, since sometimes the current_component will match the slug, and other times it will match the root_slug. BP 1.3 introduces
bp_is_current_component(), which is an all-purpose function for generously detecting your current component. It takes slugs (‘topics’), root slugs (‘support/topics’) and component names (‘forums’) as arguments. You should replace all of your plugin’s current_component/slug checks with this function instead. In other words, replace things like this:
if ( $bp->current_component == $bp->groups->slug )
if ( bp_is_current_component( $bp->groups->slug ) )
If you fail to make these changes, your plugin will stop working whenever a BP site admin creates a directory with a complex URL, or defines one of the BP_X_SLUG constants to override the non-root_slug.