BuddyPress is a great plugin for having a social network on your site. There are times when BuddyPress Private Messages need to be controlled so that some of your users can’t use them, read them or just send them. In a recent project, I have had the need to disable them for some users.
The code that I’ll show you here is not a complete code or a code used in a real solution. This tutorial will just show you a few of actions or filters that can be used to control the BuddyPress Private Messages and how to use them.
The functions that are used here to control the user capabilities are not real. They are just here so that you can understand the logic and see how you could implement something like that yourself.
BuddyPress Dependancy
If you want, you can write a plugin for it or not. If you are going to write a plugin for it, be sure to check if BuddyPress is installed and activated. You can do this simply by creating something like this:
With the function class_exists
we can check if a class is included in our code. When BuddyPress is activated, the class BuddyPress
will exist in our code. We are then safe to use other BuddyPress functions.
Remove the Private Message Button
When using the BuddyPress Private Messages component, a user can see a button “Private Message” on another user’s profile. This button can be used to compose a private message to that user.
If we have a site that uses different roles with different capabilities, one of those role could be limited in using the Private Messages. This is how we would remove that button:
Remove the Compose Form
What about the compose form? We don’t want it if our user can’t write private messages. We could just remove the navigation “Compose” (which we will do later in this tutorial), but what if for some reason our user gets to a Compose form?
Let’s control the display of that form with this code:
Don’t save or send the Private Messages
This is actually the core thing. We can always hide or remove forms and buttons, but what if a plugin uses the BuddyPress Private Messages to send them automatically? We don’t want to allow that!
As you can see here, we don’t return the $message_object
because that object is passed by reference which means that the changes will reflect on the object even outside of our function.
The only thing we need to do here is to remove the recipients from the message object. This will stop BuddyPress from sending or saving the message.
Controlling the Private Message Navigation
This is something that could come in useful for your site or project. We could have a user that should not even see the “Messages” navigation item anywhere on the profile. Or we could disallow the user from seeing their sent messages or compose form. Let’s take a look at the code:
Conclusion
There are many other actions and filters for controlling the BuddyPress Private Messages and I do encourage you to look them up. The best way to find out about the actions or filters is by downloading the BuddyPress plugin and then look at the files inside the folder buddypress/bp-messages
.
Have you had a situation like this where you had to control some parts of BuddyPress? What parts or components of BuddyPress are the most useful for you?
Become a Sponsor
Great post. Do you have an idea how one can reward users for receiving messages
That also depends on the rewarding system and what the rewards could be. I am not 100% sure, but I do think you could use a hook in the BP system to listen for new messages and then on each new message, let the rewarding system check the users and reward them accordingly.
thanks a lot for this !
Do you have any idea how to limit the number of messages.
For ex, 30 messages max per month for a specific membership.
I am looking specifically for a way to get the number of messages already sent by a user. I think I could code the rest.
Great post Igor, very helpful.
I need to allow free members to to receive and reply. Only paying members can send to any user. I’m using BuddyPress Messaging with Paid Membership Pro and am looking to restrict by role. Any guidance on this would be greatly appreciated.
Hi, How would I be able to set up several predefined private messages in buddy press and have the options to hide the current chat box?
By the way, I love your tutorials.
Hello, what about a scenario where i want only a user to admin conversation
Hi Rilwan, you can check if the current user is a regular member and check if the user he/she tries to send a message is an administrator.
Great post. Do you think its possible to show the private message button in the activity stream
Hi John, I am not sure. It probably is possible since BuddyPress is really well coded with a lot of hooks so you can easily change or add new elements to existing templates.
In case it would redirect the user to such form, you won’t have an issue, but in case you want to allow private messages directly on the activity stream, you would have some coding to do.