Skip to content

Instantly share code, notes, and snippets.

@dwanjuki
Last active August 5, 2026 15:08
Show Gist options
  • Select an option

  • Save dwanjuki/13331eeadff10343fbeb1c34bd9c0d02 to your computer and use it in GitHub Desktop.

Select an option

Save dwanjuki/13331eeadff10343fbeb1c34bd9c0d02 to your computer and use it in GitHub Desktop.
BuddyPress/BuddyBoss: Prevent non-members from posting to activity feed or reacting to activity.
<?php
/**
* BuddyPress/BuddyBoss: Prevent non-members from posting, commenting on, or liking activity.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
/**
* Block new activity updates and comments from users without a membership level.
*/
function my_pmpro_bp_activity_only_members_can_post( $activity ) {
// Only gate brand-new posts, not edits to existing ones.
if ( ! empty( $activity->id ) ) {
return;
}
// Only gate user-authored activity, never programmatic activity.
if ( ! in_array( $activity->type, array( 'activity_update', 'activity_comment' ), true ) ) {
return;
}
// Bail if PMPro is not active or there is no user to check.
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) || empty( $activity->user_id ) ) {
return;
}
// Never lock out admins and moderators, who often have no membership level.
if ( user_can( $activity->user_id, 'bp_moderate' ) ) {
return;
}
// Check the level of the user the activity belongs to, not the current user:
// the two differ for admin actions, cron, WP-CLI and other programmatic saves.
if ( pmpro_hasMembershipLevel( null, $activity->user_id ) ) {
return;
}
// Block activity.
$activity->error_type = 'wp_error';
$activity->errors->add(
'my_activity_blocked',
'You must have a membership level to post activity.'
);
}
add_action( 'bp_activity_before_save', 'my_pmpro_bp_activity_only_members_can_post' );
/**
* Hide the comment link/form on activity items from users without a membership level.
*/
function my_pmpro_bp_activity_only_members_can_comment( $can_comment ) {
// Bail if the user is already blocked from commenting.
if ( ! $can_comment ) {
return $can_comment;
}
// Bail if PMPro is not active.
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
return $can_comment;
}
// Never lock out admins and moderators.
if ( current_user_can( 'bp_moderate' ) ) {
return $can_comment;
}
// If the user does not have a membership, disable commenting.
if ( ! pmpro_hasMembershipLevel() ) {
$can_comment = false;
}
return $can_comment;
}
add_filter( 'bp_activity_can_comment', 'my_pmpro_bp_activity_only_members_can_comment' );
/*
* Disable BuddyBoss activity likes for non-members.
*/
function my_pmpro_bp_activity_only_members_can_like( $is_active ) {
// Bail if the feature is already off, or PMPro is not active.
if ( empty( $is_active ) || ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
return $is_active;
}
// Never lock out admins and moderators.
if ( current_user_can( 'bp_moderate' ) ) {
return $is_active;
}
if ( ! pmpro_hasMembershipLevel() ) {
return false;
}
return $is_active;
}
add_filter( 'bp_is_activity_like_active', 'my_pmpro_bp_activity_only_members_can_like' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment