The group extension API (1.1+) makes it very easy to add custom creation steps, edit screens, navigation items and pages to a group. It essentially allows you to create fully functional extensions to BuddyPress created groups.
The API Syntax
A group extension could be created as a standalone plugin file, or included as code within a plugin that performs other tasks. The core API code is as follows:
class My_Group_Extension extends BP_Group_Extension {
function my_group_extension() {
$this->name = 'My Group Extension';
$this->slug = 'my-group-extension';
$this->create_step_position = 21;
$this->nav_item_position = 31;
}
function create_screen() {
if ( !bp_is_group_creation_step( $this->slug ) )
return false;
?>
<p>The HTML for my creation step goes here.</p>
<?php
wp_nonce_field( 'groups_create_save_' . $this->slug );
}
function create_screen_save() {
global $bp;
check_admin_referer( 'groups_create_save_' . $this->slug );
/* Save any details submitted here */
groups_update_groupmeta( $bp->groups->new_group_id, 'my_meta_name', 'value' );
}
function edit_screen() {
if ( !bp_is_group_admin_screen( $this->slug ) )
return false; ?>
<h2><?php echo attribute_escape( $this->name ) ?></h2>
<p>Edit steps here</p>
<input type="submit" name="save" value="Save" />
<?php
wp_nonce_field( 'groups_edit_save_' . $this->slug );
}
function edit_screen_save() {
global $bp;
if ( !isset( $_POST['save'] ) )
return false;
check_admin_referer( 'groups_edit_save_' . $this->slug );
/* Insert your edit screen save code here */
/* To post an error/success message to the screen, use the following */
if ( !$success )
bp_core_add_message( __( 'There was an error saving, please try again', 'buddypress' ), 'error' );
else
bp_core_add_message( __( 'Settings saved successfully', 'buddypress' ) );
bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . '/admin/' . $this->slug );
}
function display() {
/* Use this function to display the actual content of your group extension when the nav item is selected */
}
function widget_display() { ?>
<div class="info-group">
<h4><?php echo attribute_escape( $this->name ) ?></h4>
<p>
You could display a small snippet of information from your group extension here. It will show on the group
home screen.
</p>
</div>
<?php
}
}
bp_register_group_extension( 'My_Group_Extension' );
Optional Parameters
There are some optional parameters you can set in your group extension. These all go above the constructor, so in the above example, above the my_group_extension() function.
var $visibility = 'public'; // 'public' will show your extension to non-group members, 'private' means you have to be a member of the group to view your extension. var $enable_create_step = true; // If your extension does not need a creation step, set this to false var $enable_nav_item = true; // If your extension does not need a navigation item, set this to false var $enable_edit_item = true; // If your extension does not need an edit screen, set this to false
Advanced Usage
There may be circumstances where your group extension will need a navigation item, but sometimes not. Usually it is not quite as cut and try as “true” or “false”. In these cases you can could add and extra method to the extension that will dynamically change these options on a group by group basis.
/* Place this in the constructor */
$this->enable_nav_item = $this->enable_nav_item();
/* Add this method the end of your extension class */
function enable_nav_item() {
global $bp;
/* You might want to check some groupmeta for this group, before determining whether to enable the nav item */
if ( groups_get_groupmeta( $bp->groups->current_group->id, 'settings_complete' ) )
return true;
else
return false;
}