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?

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 *