The group extension API 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 Group Extension API consists of a base class called,BP_Group_Extension which you extend in your own plugin. BP_Group_Extension does most of the work necessary to integrate your content into BP group – creating new navigation tabs, registering a step during group creation, etc.

Steps:

Use BP_Group_Extension as follows:

  1. In theme’s functions.php file, create your own class that extends BP_Group_Extension.
  2. In the __construct() method, pass an array of arguments to parent::init().
  3. Write any methods that your extension requires.
  4. Register your extension with BuddyPress using bp_register_group_extension().

Methods:

BP_Group_Extension has built-in support for a number of different customization methods, which you can override in your extension as needed.

The most prominent of these methods is display(), which BuddyPress uses to generate the output of main tab. Your display() method should echo markup, which BP will automatically place into the proper template.

For the three “screen” contexts – ‘create’, ‘edit’, and ‘admin’ – a flexible system allows you to customize the way that your settings screens work, without unnecessary reproduction of code. Your extension should, at minimum, provide the following two methods:

  • settings_screen() : Outputs the fallback markup for your create/edit/admin screens
  • settings_screen_save() : Called after changes are submitted from the create/edit/admin screens. This methods should contain the logic necessary to catch settings form submits, validate submitted settings, and save them to the database.

If your extension requires further customization to one or more of the screens, BuddyPress provides the following methods, which are context-specific:

  • create_screen()
  • create_screen_save()
  • edit_screen()
  • edit_screen_save()
  • admin_screen()
  • admin_screen_save()

If your extension contains any of these methods, BP will use them when appropriate. Otherwise, the generic settings_screen() or settings_screen_save() will be used.

Example:

In given example, we create a tab in BuddyPress group, and a setting subtab under Manage tab using Group Extension API.

/*
 * The bp_is_active( 'groups' ) check is recommended, to prevent problems
 * during upgrade or when the Groups component is disabled
 */
if ( bp_is_active( 'groups' ) ) :

class Group_Extension_Example extends BP_Group_Extension {
    /**
     * Here you can see more customization of the config options
     */
    function __construct() {
        $args = array(
            'slug' => 'add-review',
            'name' => 'Add Review',
            'nav_item_position' => 105,
            'screens' => array(
                'edit' => array(
                    'name' => 'Manage Review',
                    'slug' => 'manage-review',
                    'submit_text' => 'Submit Review', // Changes the text of the Submit button on the Edit page
                ),
                'create' => array(
                    'position' => 100,
                ),
            ),
        );
        parent::init( $args );
    }

    function display( $group_id = NULL ) {
        $group_id = bp_get_group_id();
        echo 'Here, You should content that you want to display in "Add Review" tab';
    }

    function settings_screen( $group_id = NULL ) {
        $setting = groups_get_groupmeta( $group_id, 'review_setting' );

        ?>
        Save your plugin setting here: <input type="text" name="review_setting" value="" />
        
        Welcome to your new group! You are neat.
        Save your plugin setting here: <input type="text" name="review_setting" value="" />
        <?php
    }

}
bp_register_group_extension( 'Group_Extension_Example' );

endif;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

logo