BuddyPress Activity feature is similar to a Twitter feed where you can post public messages to specific members or to all. What if you want to limit such messages between specific members? In this tutorial, we will learn how to limit BuddyPress Activity Messages between members.

For this tutorial, we will assume that some members can receive direct activity messages while other members can’t do that. For example, we won’t be able to mention ourselves. This can be then extended with whatever logic you need.

In BuddyPress, you can create such messages on the Activity page or even within your profile using the message form under Messages > Compose. If you select “This is a notice to all users”, it will be handled as a public (activity) message and not a private one.

Hooks for BuddyPress Activity Messages

There are a lot of hooks (filters or actions) in BuddyPress and a lot of them can be used even for messages. Since we will limit those messages right at the beginning, we need only two.

One is when the activity message is trying to get saved in the database and the second one is when we try to update it. This is the filter that we are going to use when we try to post an activity update.

And this is the action that we are going to use when a message tries to be saved as activity feed.

Finding the Mentioned Users

To find which users are trying to get mentioned in an activity feed, we need to search for users in the activity content that is trying to get posted.

BuddyPress already has a function that can help us for that and that is:

bp_activity_find_mentions( $content )

You can read the whole function definition on github. This function will search the content for mentions with @ and then try to find the user IDs based on those mentions. It will return an array of found user IDs.

Helper Function to check the Users

Let’s now create a helper function that we can use to check if a specific user can be mentioned or not.

Filtering the Activity Content

We will need to hook on the filter mentioned above to filter the content of an Activity message.

If we find a user that can’t be mentioned, we will break from the loop because one is enough. Then, we are setting an empty content because then the activity won’t be processed. Lastly, we also add an error message that will be displayed to the user trying to post an update.

Disabling an Activity Message on Save

To disable an activity message, we will need to hook before the message is going to be saved. If we add an error to the BP_Activity_Activity object, the saving process will be stopped.

In this function, if we find a user that can’t be mentioned, we are adding that error to the activity message.

Conclusion

BuddyPress is full of hooks and you can basically control every aspect of it. In this tutorial, we have learned how to block specific users in public messages.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.