Disable BuddyPress Components Conditionally
Do you want to disable some of the BuddyPress components based on user role or some other conditions? if you do, BuddyPress provides us with a filter hook "bp_is_active" that you can use to conditionally disable the components. It is not even necessary to filter based on role, you can filter based on anything you want.
The hook passes us two arguments $enabled, $component. The $enabled is a Boolean that decides whether the component will act as active or not? The $component is a string which can be any of the valid BuddyPress component. To disable a component, set the $enabled to false when the conditions match.
Let us see some examples.
Example:- Disable BuddyPress friends component for non logged in users
The code disables the friends component on user profiles when non logged in user visits them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * Disable BuddyPress Friends Components for visitors. * * @param bool $enabled is component enabled? * @param string $component BuddyPress Component id(e.g 'activity', 'groups', 'notifications', 'friends', 'settings', 'xprofile', 'messages', etc) * * @return mixed */ function buddydev_conditionally_disable_friends_component_for_visitors( $enabled, $component ) { if ( ! is_user_logged_in() && 'friends' == $component ) { $enabled = false ; } return $enabled; } add_filter( 'bp_is_active', 'buddydev_conditionally_disable_friends_component_for_visitors', 10, 2 ); |
Example:- Disable friends component for subscribers
You can use the following code to disable the friends functionality for the subscribers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /** * Disable BuddyPress Components conditionally based on role. * * @param bool $enabled is component enabled? * @param string $component BuddyPress Component id(e.g 'activity', 'groups', 'notifications', 'friends', 'settings', 'xprofile', 'messages', etc) * * @return mixed */ function buddydev_conditionally_disable_components( $enabled, $component ) { if ( ! is_user_logged_in() ) { return $enabled; // we do not disable component for the logged in user } $user = wp_get_current_user(); $roles = $user->roles ; // $role check for 'subscriber', 'contributor', 'author', 'editor', 'administrator' if ( 'friends' == $component && in_array( 'subscriber', $roles ) ) { //disable friends for subscribers $enabled = false; } return $enabled; } add_filter( 'bp_is_active', 'buddydev_conditionally_disable_components', 10, 2 ); |
Please do note that if you are disabling a component for a logged in user, It may provide some inconsistent experience.
For example, if you disable the notifications component for the subscriber and they get a notification because someone mentioned them, It may provide an inconsistent experience. It is easily possible to avoid such instances. You should consider the ways to make the experience seamless.
I hope that you can extend the above function for multiple use case.