Disable BuddyPress group deletion by Non Site Admin
We received this question from Andre? How would you disable the BuddyPress group deletion by group admins and only allow site admins to delete a group? I believe, It may be helpful for others, so posting the code here
Disabling the delete option in the Group -> Manage screen:-
1 2 3 4 5 6 7 8 9 10 | function buddydev_disable_group_delete_by_non_site_admin() { if ( ! bp_is_group() || is_super_admin() ) { return; } $parent = groups_get_current_group()->slug . '_manage'; bp_core_remove_subnav_item( $parent, 'delete-group', 'groups' ); } add_action( 'groups_setup_nav', 'buddydev_disable_group_delete_by_non_site_admin' ); |
In the above code we check that we are on a group page and if the current user is not site admin, we remove the "delete" from the manage screen. Here is a screenshot:-
The above code works but there is a problem. When you click on"Manage", It will give 404 as the redirect to the default sub nav is stopped. It seems there is a bug in BuddyPress that removes the screen callback. The solution, we reattach it again. Here is our final, updated code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Disable group deletion by non site admins. */ function buddydev_disable_group_delete_by_non_site_admin() { if ( ! bp_is_group() || is_super_admin() ) { return; } $parent = groups_get_current_group()->slug . '_manage'; bp_core_remove_subnav_item( $parent, 'delete-group', 'groups' ); // BuddyPress seems to have a bug, the same screen function is used for all the sub nav in group manage // so above code removes the callback, let us reattach it // if we don't , the admin redirect will not work. if ( function_exists( 'groups_screen_group_admin' ) ) { add_action( 'bp_screens', 'groups_screen_group_admin', 2 ); } } add_action( 'groups_setup_nav', 'buddydev_disable_group_delete_by_non_site_admin' ); |
That's it. Put this code in your bp-custom.php and the future of all BuddyPress groups now depends on you, the site admin 🙂
Thank you very much! I'm going to link this to my Buddypress.org post and will be making a contribution to your site for this assistance. Best – Andre
Thank you Andre. It is a pleasure to help 🙂
Thanks ,
Is it possible to delet all groups at once from backend i have test some plugin but none of them work
Can you help me.
Hi Nabeel,
My suggestions will be to take a backup of the database, Then truncating wp_bp_groups, wp_bp_groupmeta tables(prefixi wp may be different for you).
After that, Please visit Dashboard->Tools->BuddyPress and check " Repair total groups count for each member." and repair it.
That should be the fastest way to do it.
Regards
Brajesh
Hi,
When i added this code to bp-custom.php file it works functionally(non admin users cannot delete the groups that they created) but at the top of the page it displays an error message that can be seen below. Can you help me? Thanks. (Note: It gives error in both PHP 5.6 and 7.2 versions.)
Hi Kerem,
It happened due to a change in BuddyPress 3.0. I have updated the code to fix it.
Regards
Brajesh