Skip to:
Content
Pages
Categories
Search
Top
Bottom

’BP_Group_Extension’ not found when using Group Extension API in bp-custom.php


  • notpoppy
    Participant

    @notpoppy

    I am trying to create custom tabs with their own content in different groups.

    Looking around the forum it seems this can be done using the Group Extension API.

    According to this thread it should be possible to do it by adding the API code (copied below) into bp-custom.php.

    However when I do this I get the following error:

    Fatal error: Class ‘BP_Group_Extension’ not found in (directory)bp-custom.php

    This appears to be because it is loading after Buddypress. The codex supplies information on how to fix this for building a plugin but I’m not sure how to adapt this for my code in bp-custom.php.

    Can anyone help me do this?

    Here’s the code I’m using in bp-custom.php which is as found on the Group Extension API page:

    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;
    ?>
    
    The HTML for my creation step goes here.
    
    <?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>name ) ?></h2>
    
    Edit steps here
    
    <?php
    wp_nonce_field( 'groups_edit_save_' . $this->slug );
    }
    
    function edit_screen_save() {
    global $bp;
    
    if ( !isset( $_POST ) )
    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>name ) ?></h4>
    
    You could display a small snippet of information from your group extension here. It will show on the group
    home screen.
    
    </div>
    <?php
    }
    }
    bp_register_group_extension( 'My_Group_Extension' );

    Incidentally, I note there is a plugin designed to achieve what I’m trying to do but as it’s only in beta I can’t use it on a live site yet.

Viewing 7 replies - 1 through 7 (of 7 total)

  • r-a-y
    Keymaster

    @r-a-y

    @notpoppy – You have the right idea that your code is loading before BuddyPress and you are also correct to have read the Checking if BuddyPress is active codex article.

    Now, you just need to connect the dots!

    1. Create a new folder (eg. /my-bp-plugin/).
    2. Copy and paste the contents of what you wrote above into a new file (eg. /my-bp-plugin/my-bp-plugin.php).
    3. Now, copy and paste the contents of the codex article into a new file (eg. /my-bp-plugin/loader.php) into the folder you just created. Make sure to change the file path and name to the ones you created above as well as the meta data at the top of the file.
    4. You should now have a folder with two files. Move this folder into the /wp-content/plugins/ directory, navigate to the WP admin dashboard and activate the plugin!

    If you have any other questions, let me know.


    notpoppy
    Participant

    @notpoppy

    Thanks very much for the quick reply – will report back when I’ve tried it out.


    notpoppy
    Participant

    @notpoppy

    @r-a-y OK I’ve successfully built the plugin and I now have a new tab called ”My Group Extension” which appears in every group.

    I see I can use the following to display text in each tab:

    `function display() {
    /* Use this function to display the actual content of your group extension when the nav item is selected */
    }`

    However I need to have the tabs only appear in certain groups, and display different information in each group. How can I achieve this?

    Thanks again for your help so far.


    notpoppy
    Participant

    @notpoppy

    I’ve tried to follow the instructions in Group Extension API on how to restrict the tab to certain pages. I believe I’ve integrated the code correctly but it seems I need to do something else to tell it which groups I want to restrict it to but I can’t tell what.

    I’ve had difficult posting the code on here – I keep getting blank pages – but I’ll try to post it in subsequent posts.

    I need to know what I should do in order to:

    1. Restrict the tabs so they only appear in certain groups
    2. Customise the content of the tabs so it’s different for each page


    notpoppy
    Participant

    @notpoppy

    OK it won’t let me post code at all for some reason. I’ve added the code described in Group Extension API under Advanced Usage.


    r-a-y
    Keymaster

    @r-a-y

    @notpoppy – For this line:

    `if ( groups_get_groupmeta( $bp->groups->current_group->id, ‘settings_complete’ ) )`

    You could probably change that to do your check against group ID

    eg.

    `if ( bp_get_group_id() == MY_GROUP_ID )
    return true;
    elseif ( bp_get_group_id() == ANOTHER_GROUP_ID )
    return true;
    else
    return false;
    `

    etc.

    If you want to do your check by group name, try using `bp_get_group_name()` in place of `bp_get_group_id()`.

    This is untested, but hopefully this gives you a few ideas to work with.


    notpoppy
    Participant

    @notpoppy

    @r-a-y OK that didn’t quite work for me but this did:

    `function enable_nav_item () {

    global $bp;

    if ( $bp->groups->current_group->id == MY_GROUP_ID )
    return true;
    else
    return false;
    }`

    I’m not sure why this worked – I got the idea from this old thread. It would be good to know why if someone can explain it to me.

    The way I see it, in order to complete this I now need to create individual versions of the complete code for each group where I require these custom tabs. I am sure there’s vastly more elegant ways of doing this but with my meagre PHP knowledge this is the best I can muster for now!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘’BP_Group_Extension’ not found when using Group Extension API in bp-custom.php’ is closed to new replies.
Skip to toolbar