-1
<?php
/*
Plugin Name: Custom BuddyPress Group Tabs
Description: Adds custom tabs to BuddyPress groups
Version: 1.0
Author: Bing
*/

// create the custom database table upon plugin activation
function asdf_group_tabs_install() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'custom_group_tabs';
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        tab_name text NOT NULL,
        tab_slug text NOT NULL,
        tab_content longtext NOT NULL,
        group_id mediumint(9) NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
}
register_activation_hook( __FILE__, 'asdf_group_tabs_install' );

// create a new standalone menu item in the WordPress admin dashboard
function asdf_group_tabs_admin_menu() {
    add_menu_page(
        'BuddyPress Group Tabs',
        'Group Tabs',
        'manage_options',
        'buddypress-group-tabs',
        'asdf_group_tabs_admin_page',
        'dashicons-groups'
    );
}
add_action( 'admin_menu', 'asdf_group_tabs_admin_menu' );

// display the admin page for managing custom group tabs
function asdf_group_tabs_admin_page() {
    // check user capabilities
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    // check if the user has submitted the form
    if ( isset( $_POST['submit'] ) ) {
        // process form data
        $tab_name = sanitize_text_field( $_POST['tab_name'] );
        $tab_slug = sanitize_title( $_POST['tab_slug'] );
        $tab_content = wp_kses_post( $_POST['tab_content'] );
        $group_id = intval( $_POST['group_id'] );

        // validate data
        if ( empty( $tab_name ) || empty( $tab_slug ) || empty( $group_id ) ) {
            // display an error message
            // ...
        } else {
            // save the custom tab data to the database
            global $wpdb;
            $table_name = $wpdb->prefix . 'custom_group_tabs';
            $wpdb->insert(
                $table_name,
                array(
                    'tab_name' => $tab_name,
                    'tab_slug' => $tab_slug,
                    'tab_content' => $tab_content,
                    'group_id' => $group_id
                )
            );
        }
    }
 
    ?>
    <div class="wrap">
        <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
        <form method="post">
            <table class="form-table">
                <tr>
                    <th scope="row"><label for="tab_name">Tab Name</label></th>
                    <td><input name="tab_name" type="text" id="tab_name" value="" class="regular-text"></td>
                </tr>
                <tr>
                    <th scope="row"><label for="tab_slug">Tab Slug</label></th>
                    <td><input name="tab_slug" type="text" id="tab_slug" value="" class="regular-text"></td>
                </tr>
                <tr>
                    <th scope="row"><label for="tab_content">Tab Content</label></th>
                    <td><textarea name="tab_content" rows="5" cols="46" id="tab_content"></textarea></td>
                </tr>
                <tr>
                    <th scope="row"><label for="group_id">Group ID</label></th>
                    <td><input name="group_id" type="number" step="1" min="1" id="group_id" value="" class="small-text"></td>
                </tr>
            </table>
            <?php submit_button( __( 'Save Tab', 'textdomain' ) ); ?>
        </form>

        <?php
        // retrieve custom tabs from the database
        global $wpdb;
        $table_name = $wpdb->prefix . 'custom_group_tabs';
        $results = $wpdb->get_results( "SELECT * FROM {$table_name}" );
        
        // display existing custom tabs
        if ( ! empty( $results ) ) {
            echo '<h2>Existing Custom Tabs</h2>';
            echo '<table class="widefat fixed striped">';
            echo '<thead><tr><th>Tab Name</th><th>Tab Slug</th><th>Group ID</th><th>Actions</th></tr></thead>';
            echo '<tbody>';
            foreach ( $results as $row ) {
                echo '<tr>';
                echo '<td>' . esc_html( $row->tab_name ) . '</td>';
                echo '<td>' . esc_html( $row->tab_slug ) . '</td>';
                echo '<td>' . intval( $row->group_id ) . '</td>';
                echo '<td><a href="#">Edit</a> | <a href="#">Delete</a></td>';
                echo '</tr>';
            }
            echo '</tbody>';
            echo '</table>';
        }
        ?>
    </div>
    <?php
}

// create custom group tabs on the front-end
function asdf_create_custom_group_tabs() {
    global $bp;
    
    // check if we are on a group page
    if ( bp_is_group() ) {
        // retrieve custom tabs from the database
        global $wpdb;
        $table_name = $wpdb->prefix . 'custom_group_tabs';
        $results = $wpdb->get_results( "SELECT * FROM {$table_name}" );
        
        // create custom tabs
        foreach ( $results as $row ) {
            // check if the current group is the one the custom tab should be assigned to
            if ( bp_get_current_group_id() == intval( $row->group_id ) ) {
                // create the custom tab
                bp_core_new_subnav_item( array(
                    'name' => $row->tab_name,
                    'slug' => $row->tab_slug,
                    'parent_slug' => $bp->groups->current_group->slug,
                    'parent_url' => bp_get_group_permalink( $bp->groups->current_group ),
                    'screen_function' => 'asdf_show_custom_tab_content',
                    'position' => 90,
                    'user_has_access' => true
                ) );
            }
        }
    }
}
add_action( 'bp_setup_nav', 'asdf_create_custom_group_tabs', 100 );

// display custom tab content on the front-end
function asdf_show_custom_tab_content() {
    // retrieve custom tabs from the database
    global $wpdb;
    $table_name = $wpdb->prefix . 'custom_group_tabs';
    $results = $wpdb->get_results( "SELECT * FROM {$table_name}" );
    
    // find the current custom tab
    foreach ( $results as $row ) {
        if ( bp_current_action() == $row->tab_slug ) {
            // display the custom tab content
            echo do_shortcode( $row->tab_content );
            break;
        }
    }
}

4
  • Do you mean the URLs generated by bp_get_group_permalink( $bp->groups->current_group ) don't work? I can't see any other URLs in the code at first glance. What's wrong with them? Do they look like correct BuddyPress group URLs? How do they compare to the correct URLs for those groups?
    – Rup
    Aug 16 at 8:14
  • Where did you get this code from, who's Bing? Can you ask them for help?
    – Rup
    Aug 16 at 8:15
  • got it from chatGPT, @rup the URLs have the correct format but once the created tab is clicked, the url accessed is producing a 404 error
    – goz
    Aug 18 at 3:39
  • Is there a way to maybe register the link so that WordPress recognizes it as a valid link
    – goz
    Aug 18 at 3:41

1 Answer 1

0

you can use the code below, I edited it to work

<?php
/*
Plugin Name: Custom BuddyPress Group Tabs
Description: Adds custom tabs to BuddyPress groups
Version: 1.0
Author: Bing
*/

// create the custom database table upon plugin activation
function asdf_group_tabs_install() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'custom_group_tabs';
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        tab_name text NOT NULL,
        tab_slug text NOT NULL,
        tab_content longtext NOT NULL,
        group_id mediumint(9) NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
}
register_activation_hook( __FILE__, 'asdf_group_tabs_install' );

// create a new standalone menu item in the WordPress admin dashboard
function asdf_group_tabs_admin_menu() {
    add_menu_page(
        'BuddyPress Group Tabs',
        'Group Tabs',
        'manage_options',
        'buddypress-group-tabs',
        'asdf_group_tabs_admin_page',
        'dashicons-groups'
    );
}
add_action( 'admin_menu', 'asdf_group_tabs_admin_menu' );

// display the admin page for managing custom group tabs
function asdf_group_tabs_admin_page() {
    // check user capabilities
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    // check if the user has submitted the form
    if ( isset( $_POST['submit'] ) ) {
        // process form data
        $tab_name = sanitize_text_field( $_POST['tab_name'] );
        $tab_slug = sanitize_title( $_POST['tab_slug'] );
        $tab_content = wp_kses_post( $_POST['tab_content'] );
        $group_id = intval( $_POST['group_id'] );

        // validate data
        if ( empty( $tab_name ) || empty( $tab_slug ) || empty( $group_id ) ) {
            // display an error message
            // ...
        } else {
            // save the custom tab data to the database
            global $wpdb;
            $table_name = $wpdb->prefix . 'custom_group_tabs';
            $wpdb->insert(
                $table_name,
                array(
                    'tab_name' => $tab_name,
                    'tab_slug' => $tab_slug,
                    'tab_content' => $tab_content,
                    'group_id' => $group_id
                )
            );
        }
    }
 
    ?>
    <div class="wrap">
        <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
        <form method="post">
            <table class="form-table">
                <tr>
                    <th scope="row"><label for="tab_name">Tab Name</label></th>
                    <td><input name="tab_name" type="text" id="tab_name" value="" class="regular-text"></td>
                </tr>
                <tr>
                    <th scope="row"><label for="tab_slug">Tab Slug</label></th>
                    <td><input name="tab_slug" type="text" id="tab_slug" value="" class="regular-text"></td>
                </tr>
                <tr>
                    <th scope="row"><label for="tab_content">Tab Content</label></th>
                    <td><textarea name="tab_content" rows="5" cols="46" id="tab_content"></textarea></td>
                </tr>
                <tr>
                    <th scope="row"><label for="group_id">Group ID</label></th>
                    <td><input name="group_id" type="number" step="1" min="1" id="group_id" value="" class="small-text"></td>
                </tr>
            </table>
            <?php submit_button( __( 'Save Tab', 'textdomain' ) ); ?>
        </form>

        <?php
        // retrieve custom tabs from the database
        global $wpdb;
        $table_name = $wpdb->prefix . 'custom_group_tabs';
        $results = $wpdb->get_results( "SELECT * FROM {$table_name}" );
        
        // display existing custom tabs
        if ( ! empty( $results ) ) {
            echo '<h2>Existing Custom Tabs</h2>';
            echo '<table class="widefat fixed striped">';
            echo '<thead><tr><th>Tab Name</th><th>Tab Slug</th><th>Group ID</th><th>Actions</th></tr></thead>';
            echo '<tbody>';
            foreach ( $results as $row ) {
                echo '<tr>';
                echo '<td>' . esc_html( $row->tab_name ) . '</td>';
                echo '<td>' . esc_html( $row->tab_slug ) . '</td>';
                echo '<td>' . intval( $row->group_id ) . '</td>';
                echo '<td><a href="#">Edit</a> | <a href="#">Delete</a></td>';
                echo '</tr>';
            }
            echo '</tbody>';
            echo '</table>';
        }
        ?>
    </div>
    <?php
}

// create custom group tabs on the front-end
function asdf_create_custom_group_tabs() {
    global $bp;
    
    // check if we are on a group page
    if ( bp_is_group() ) {
        // retrieve custom tabs from the database
        global $wpdb;
        $table_name = $wpdb->prefix . 'custom_group_tabs';
        $results = $wpdb->get_results( "SELECT * FROM {$table_name}" );
        
        // create custom tabs
        foreach ( $results as $row ) {
            // check if the current group is the one the custom tab should be assigned to
            if ( bp_get_current_group_id() == intval( $row->group_id ) ) {
                // create the custom tab
                bp_core_new_subnav_item( array(
                    'name' => $row->tab_name,
                    'slug' => $row->tab_slug,
                    'parent_slug' => $bp->groups->current_group->slug,
                    'parent_url' => bp_get_group_permalink( $bp->groups->current_group ),
                    'screen_function' => 'asdf_show_custom_tab_content',
                    'position' => 90,
                    'user_has_access' => true
                ) );
            }
        }
    }
}
add_action( 'bp_setup_nav', 'asdf_create_custom_group_tabs', 100 );

// display custom tab content on the front-end
function asdf_show_custom_tab_content() {
    // Add a title and a content to the custom tab page
    add_action( 'bp_template_title', 'asdf_show_custom_tab_title' );
    add_action( 'bp_template_content', 'asdf_show_custom_tab_contentt' );

    // Load a BuddyPress template
    bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'groups/single/plugins' ) );
}

function asdf_show_custom_tab_title() {
    // Display the title of the custom tab
    echo 'Custom Tab';
}

function asdf_show_custom_tab_contentt() {
    // retrieve custom tabs from the database
    global $wpdb;
    $table_name = $wpdb->prefix . 'custom_group_tabs';
    $results = $wpdb->get_results( "SELECT * FROM {$table_name}" );
    
    // find the current custom tab
    foreach ( $results as $row ) {
        if ( bp_current_action() == $row->tab_slug ) {
            // display the custom tab content
            echo do_shortcode( $row->tab_content );
            break;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.