0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re running a social network with BuddyPress, you might want to showcase the popularity of certain activity posts by displaying the number of comments they have received. However, you may also want to limit this feature to only logged in users. Fortunately, this is a relatively simple customization that can be accomplished with a few lines of code. In this article, we’ll show you how to add a comment count to your BuddyPress activity feed, and restrict it to logged in users only. Let’s dive in!

					function wpturbo_comment_count_activity_filter($sql) {
    if ( is_user_logged_in() ) {
        global $bp;
        $sql = str_replace("( comment_approved = '1' )", "( comment_approved = '1' ) AND user_id = {$bp->loggedin_user->id}", $sql);
    } else {
        $sql = str_replace("( comment_approved = '1' )", "( comment_approved = '1' ) AND user_id = -1", $sql);
    }
    return $sql;
}
add_filter('bp_core_get_user_activity_comment_count', 'wpturbo_comment_count_activity_filter');
				

The goal of this code snippet is to only show the activity comment count on the BuddyPress user profile page to logged-in users. This is accomplished by manipulating the SQL query that BuddyPress uses to retrieve the comment count.

To achieve this, first, we create a function called wpturbo_comment_count_activity_filter(). This function takes the SQL query that BP uses to retrieve the comment count, and modifies it based on whether the user is logged in or not.

If the user is logged in, we retrieve their user ID using the $bp->loggedin_user->id global variable. We replace the original SQL query to add an additional condition to retrieve only the comment count that belongs to the logged-in user. Here’s the code:

global $bp;
$sql = str_replace("( comment_approved = '1' )", "( comment_approved = '1' ) AND user_id = {$bp->loggedin_user->id}", $sql);

In this snippet, we use the str_replace() function along with a regular expression to replace the initial query string with our new query string. Note that we compare the user_id of the activity comment to the loggedin_user->id.

If the user is not logged in, we only want to retrieve the overall comment count for the activity, and not any specific user. In this case, we replace the user_id with -1. Here’s the code:

$sql = str_replace("( comment_approved = '1' )", "( comment_approved = '1' ) AND user_id = -1", $sql);

Finally, we hook this function onto the bp_core_get_user_activity_comment_count filter by using the add_filter() function. This ensures that our modified SQL query is used to retrieve the comment count on the user’s profile page. When the filter triggers, the function wpturbo_comment_count_activity_filter() is executed and the query is modified according to the user’s logged-in status.

Register an account to save your snippets or go Pro to get more features.