phpwordpressbuddypress

New BuddyPress subnav item leads to 404, but only in group


The user version works perfectly. The group version correctly removes the existing tab and adds a new one, but the new tab leads to a 404 page when clicked. I was able to replicate on a fresh Docker install of WordPress 6.5.4 and BuddyPress 12.5.0 with no other plugins (besides the one below, which is one file) and the Twenty Twenty-Four theme. Thanks in advance for any help.


<?php
/*
Plugin Name: BuddyPress Group Test
Plugin URI: n/a
Description: Test adding group tab
Version: 0.0.1
Author: n/a
Author URI: n/a
License: AGPLv3
*/

add_action('bp_actions', function()
{   
    if(bp_is_group())
    {
        $group_slug = bp_get_current_group_slug();
        bp_core_remove_subnav_item("{$group_slug}_manage", 'group-avatar', 'groups');
    }
    else
    {
        bp_core_remove_subnav_item('profile', 'change-avatar', 'members');
    }
});

add_action('bp_setup_nav', function()
{
    $bp = buddypress();
    if(bp_is_group())
    {
        $group_slug = bp_get_current_group_slug();
        $tab = bp_core_new_subnav_item(
        [
            'name' => 'Change Group Photo', 
            'slug' => 'change-group-photo', 
            'parent_url' => trailingslashit(bp_get_group_permalink($group_slug)) . 'admin/', 
            'parent_slug' => "{$group_slug}_manage",
            'position' => 30,
            'screen_function' => function() use($group_slug)
            {
                bp_core_load_template(apply_filters('bp_core_template_plugin', 'groups/single/plugins'));
                add_action('bp_template_content', function()
                {
                    echo '<h1>GROUP</h1>';
                });
            },
        ]);
    }
    else
    {
        $tab = bp_core_new_subnav_item(
        [
            'name' => 'Change Profile Photo', 
            'slug' => 'change-profile-photo', 
            'parent_url' => trailingslashit($bp->loggedin_user->domain . $bp->profile->slug), 
            'parent_slug' => $bp->profile->slug,
            'position' => 30,
            'screen_function' => function()
            {
                bp_core_load_template(apply_filters('bp_core_template_plugin', 'members/single/plugins'));
                add_action('bp_template_content', function()
                {
                    echo '<h1>USER</h1>';
                });
            },
        ]);
    }
}, 10);


Solution

  • OK, I solved it, after way more effort than should be required. You have to set the subnav item’s screen_function to ‘groups_screen_group_admin’, then add a hook to ‘bp_screens’ that checks for your tab name and returns if not found, then hook your actual HTML to ‘bp_after_group_home_content’.

    <?php
    /*
    Plugin Name: BuddyPress Group Test
    Plugin URI: https://zombo.com
    Description: Test adding group tab
    Version: 1.0
    Author: Kenny Jackson
    Author URI: https://kkjdroid.github.io
    License: AGPLv3
    */
     
    add_action('bp_actions', function()
    {   
        if(bp_is_group())
        {
            $group_slug = bp_get_current_group_slug();
            bp_core_remove_subnav_item("{$group_slug}_manage", 'group-avatar', 'groups');
     
        }
        else
        {
            bp_core_remove_subnav_item('profile', 'change-avatar', 'members');
        }
    }, 8);
     
    add_action('bp_setup_nav', function()
    {
        $bp = buddypress();
        if(bp_is_group())
        {
     
            $group_slug = bp_get_current_group_slug();
            $args =
            [
                'name' => 'Change Group Photo',
                'slug' => 'change-group-photo',
                'rewrite_id' => 'bp_group_manage_change_group_photo',
                'parent_slug' => "{$group_slug}_manage",
                'position' => 81,
                'user_has_access' => true,
                'show_in_admin_bar' => true,
                'no_access_url' => bp_get_group_permalink($group_slug),
                'screen_function' => 'groups_screen_group_admin'
            ];
            $tab = bp_core_new_subnav_item($args, 'groups');
        }
        else
        {
            $tab = bp_core_new_subnav_item(
            [
                'name' => 'Change Profile Photo', 
                'slug' => 'change-profile-photo',
                'parent_slug' => $bp->profile->slug,
                'position' => 30,
                'user_has_access' => true,
                'screen_function' => function()
                {
                    bp_core_load_template(apply_filters('bp_core_template_plugin', 'members/single/plugins'));
                    add_action('bp_template_content', function()
                    {
                        echo '<h1>USER</h1>';
                    });
                },
            ]);
        }
    }, 100);
     
    add_action('bp_screens', function()
    {
        if ( 'change-group-photo' != bp_get_group_current_admin_tab() ) {
            return false;
        }
        bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'groups/single/plugins' ) );
        add_action('bp_after_group_home_content', function()
        {
            echo '<h1>GROUP</h1>';
        });
    });