modemlooper avatar
About Blog

BuddyPress No Comment on Activity of not a Group Memeber

1 min read
buddypress wordpress php
/**
 * Filter off comment button if not a member of group
 *
 * @param  boolean $can_comment
 * @param  string $activity_type
 * @return boolean
 */
function otc_hide_comment_button_non_members( $can_comment, $activity_type ) {
	global $activities_template;

	if ( ! is_super_admin() && 'groups' === $activities_template->activity->component && ! groups_is_user_member( bp_loggedin_user_id(), bp_get_activity_item_id() ) ) {
		$can_comment = false;
	}

	return $can_comment;
}
add_filter( 'bp_activity_can_comment', 'otc_hide_comment_button_non_members', 10, 2 );

/**
 * Filter off comment reply if not a member of group
 *
 * @param  boolean $can_reply
 * @param  string $activity_type
 * @return boolean
 */
function modemlooper_hide_reply_button_non_members( $can_reply, $comment ) {

	$activities = bp_activity_get_specific( array( 'activity_ids' => $comment->item_id ) );

	$activity = $activities['activities'][0];

	if ( ! is_super_admin() && 'groups' === $activity->component && ! groups_is_user_member( bp_loggedin_user_id(), $activity->item_id ) )
		$can_reply = false;

	return $can_reply;
}
add_filter( 'bp_activity_can_comment_reply', 'modemlooper_hide_reply_button_non_members', 10, 2 );