Checking for Active Components in Plugins or Themes
BuddyPress plugins (and sometimes themes), in order to extend (or display the content of) BuddyPress components generally use their hooks, their functions, template tags or even template parts. In order to avoid errors, it’s even more important to check for active components in BuddyPress 1.7.
On a fresh install, version 1.7 only activates the Activity Streams and the Extended Profiles components. I think it’s a very interesting move because it shows the site administrator that BuddyPress is a set of components he can activate or not in order to build his very own BuddyPress powered community.
It’s also requiring plugin or theme authors to be more vigilant than before as when running the installation wizard most site administrators used to simply hit the button “Save & Next” leaving all the components activated.
So to prevent errors, i think it’s important we (it’s also a note to myself) check that the component is active when we’re planning to extend its behavior or when we just need to use one of its functions.
bp_is_active
To do so, since version 1.5, we can use the bp_is_active() function. It’s located in the /bp-core/bp-core-template.php file at line 1072 (in 1.7-beta1) and needs one parameter : the component identifier. This function uses the global $bp in order to return true or false depending on whether the component is active or not.
Here’s the list of the different identifiers :
- Extended Profiles = ‘xprofile’
- Account Settings = ‘settings’
- Friend Connections = ‘friends’
- Private Messaging = ‘messages’
- Activity Streams = ‘activity’
- User Groups = ‘groups’
- Group Forums = ‘forums’
- Site Tracking = ‘blogs’
Examples of use
If for some reason we want to display a particular member’s profile field, let’s check the Extended Profiles component is active.
if ( bp_is_active( 'xprofile' ) ) $fullname = xprofile_get_field_data( 1, $user_id );
If our plugin needs to deal with user settings to allow members to customize its options, it can add a new sub navigation item to the user settings navigation. If it doesn’t check if the User Settings component is enabled, it will cause a white screen of death on the front end and on the back end!!
if( bp_is_active( 'settings' ) { bp_core_new_subnav_item( array( 'name' => __( 'BP Plugin User Settings', 'bp-checkins' ), 'slug' => 'bp-plugin-user-settings', 'parent_slug' => bp_get_settings_slug(), 'parent_url' => trailingslashit( bp_loggedin_user_domain() . bp_get_settings_slug() ), 'screen_function' => 'bp_plugin_screen_settings_menu', 'position' => 40, 'user_has_access' => bp_is_my_profile() ) ); }
If we plan to build a widget to display the friends of the logged in or the displayed user, let’s not forget to check the site administrator activated the Friends component.
function bp_show_friends_register_widgets() { if( bp_is_active( 'friends' ) ) add_action('widgets_init', create_function('', 'return register_widget("BP_Show_Friends_Widget");') ); } add_action( 'bp_register_widgets', 'bp_show_friends_register_widgets' );
A theme can choose to display the number of unread private messages of the logged in member in its header. Again, it needs to check if the component is active, or else, after its header, no more content will be displayed.
<?php if( bp_is_active( 'messages' ):?> <div class="tile-messages"> <a href="<?php echo bp_loggedin_user_domain() ?>"><?php echo messages_get_unread_count(); ?></a> </div> <?php endif;?>
Our plugin may need to take benefit of the Activity Streams component, so before recording a new activity, we should check it’s available.
if( bp_is_active('activity') ) bp_activity_add( $args );
If our plugin needs to use the Group Extension API, we should first check the User Groups component is active.
class BP_Plugin_Component extends BP_Component { function __construct() {} function includes() { // Files to include $includes = array( 'includes/bp-plugin-functions.php', 'includes/bp-plugin-templatetags.php' ); /* includes the Group API only if... ...the User Groups component is available */ if( bp_is_active( 'groups' ) ) $includes[] = 'includes/bp-plugin-group-class.php'; } function setup_globals() {} function setup_nav() {} function setup_admin_bar() {} } function bp_plugin_load_core_component() { global $bp; $bp->bp_plugin = new BP_Plugin_Component; } add_action( 'bp_loaded', 'bp_plugin_load_core_component' );
A theme can include a template to customize the functionality of the blog home page by adding an area to display the latest group forum topics. Here, we definitely need to check for the BuddyPress forum component as it will be soon possible to use bbPress 2.3 to power group forums. The error can confuse the site administrator as he won’t understand why he gets an error as the forum of his groups are working great.
<?php if( bp_is_active( 'forums' ) ):?> <div class="front-box-child"> <?php if ( bp_has_forum_topics( 'per_page=5' ) ) : ?> <ul> <?php while ( bp_forum_topics() ) : bp_the_forum_topic(); ?> <li><?php bp_the_topic_title(); ?></li> <?php endwhile; ?> </ul> <?php endif;?> </div> <?php endif;?>
If our plugin requires a component or more to be active to run, we can catch the admin_notices hook to invite the site administrator to activate the needed component(s) that his config is missing, i’ve found these interesting lines in BuddyPress Courseware.
function bpsp_check() { /* beginning of extract */ foreach( array( 'groups', 'activity', 'xprofile', 'forums', 'messages' ) as $c ) if( !bp_is_active( $c ) ) $messages[] = sprintf( __( 'BuddyPress Courseware dependency error: <a href="%1$s">%2$s has to be activated</a>!', 'bpsp' ), admin_url( 'admin.php?page=bp-general-settings' ), $c ); /* end of extract */ } add_action('admin_notices', 'bpsp_check' );