vapvarun icon

Function to add a new tab to display own groups in the BuddyPress profile

vapvarun | PRO | 12/01/23 05:20:56 PM UTC (Edited) | 0 ⭐ | 1741 👁️ | Never ⏰ | [Buddypress, own group]
PHP |

1.88 KB

|

Software

|

0 👍

/

0 👎

// Function to add a new tab in the BuddyPress profile
function vap_add_groups_created_by_user_tab() {
    global $bp;
 
    bp_core_new_nav_item( array(
        'name' => __('Own Groups', 'buddypress'), // The name of your tab
        'slug' => 'own-groups', // The slug (URL segment) for the tab
        'screen_function' => 'vap_groups_created_by_user_screen', // The function that will load the new tab content
        'position' => 50,
        'parent_url'      => bp_loggedin_user_domain() . '/own-groups/',
        'parent_slug'     => $bp->profile->slug,
    ));
}
add_action( 'bp_setup_nav', 'vap_add_groups_created_by_user_tab' );
 
// Callback function for the new tab content
function vap_groups_created_by_user_screen() {
    add_action( 'bp_template_title', 'vap_groups_created_by_user_title' );
    add_action( 'bp_template_content', 'vap_groups_created_by_user_content' );
    bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
 
// Function to set the title of your new tab page
function vap_groups_created_by_user_title() {
    echo 'My Created Groups';
}
 
// Function to display the groups created by the user
function vap_groups_created_by_user_content() {
    // Get the displayed user's ID
    $user_id = bp_displayed_user_id();
 
    // Set up the parameters for the group loop
    $args = array(
        'user_id' => $user_id, // Filter to show only groups the user has created
        'per_page' => 10, // How many groups to display per page
        // Add other parameters as needed
    );
 
    // Start the BuddyPress groups loop with these arguments
    if ( bp_has_groups( $args ) ) {
        bp_get_template_part( 'groups/groups-loop' ); // Use the groups loop template
    } else {
        echo 'No groups found.';
    }
}
 
// Add the combined code to your theme's functions.php file or a custom plugin
 

Comments