BuddyDev

Add Send Private Message Button In Members Directory on a BuddyPress Network

Since I did it for our upcoming HiBuddy Theme, I thought it might be useful for you too. I am sharing the code on how to add send private message button in members directory below (You can actually add it anywhere).

Step 1:- Find the User ID

Find the User to whom we want to send the message. I wrote a method to return the user_id depending on whether we are in the members loop or on a user's profile. You can extend it if you want.

That's simple. It will return the current user id in the members loop or the displayed user id.  Since I want to show the link to logged in users only,  I added the first line to test whether the user is logged in or not.

Step 2:- Build the URL for sending private message

That code will give us a url, which we will use later to generate the send private message button

Step 3:- Generate the Send Private Message Button

The above function generates the html for the button and returns it. Let us build another function to echo that button. We are doing it for the sake of simplicity.

Step 4:- Echoing the button

Now, We have everything in place, we just need to hook it to  BuddyPress members directory items. BuddyPress themes normally provide the hook 'bp_directory_members_actions' to which we can attach our generated button.

Step 5:- Show the button in members directory

put this line in functions.php ( Do not put the code below to your bp-custom.php, that will generate a fatal error )

You can put all the code to functions.php or bp-custom.php. If you are putting the code to bp-custom.php, please avoid putting the code in step 5 there.

So, Here is what you will get.

As you see, the buttons looks a little bit overlapped. By using 'send-private-message' class in css, we can easily fix that.

Here is how I fixed for bp-default theme.

 

That actually made it look quiet good in bp-default theme. Here is a screenshot ( I know you want the proof 🙂 ).

So, what do you guys think? Is it a good idea to provide send private message  button in the members directory ? Please do share your views and feedback in comments 🙂

20 Responses to Add Send Private Message Button In Members Directory on a BuddyPress Network

  • Very cool @sbrajesh , this has been requested by so many users on buddypress.org. Thank you again for sharing an awesome solution!

  • Yes Brajesh, that's a very useful addition. I like it – add it's working perfect. Thanks!

  • I like the context user ID idea – will use that elsewhere too, thanks Brajesh 🙂

  • Really cool @sbrajesh … 🙂

    How can i add the button to the friends section in every user profile?

  • GREAT solution, thanks so much for this great code.

  • Thank you Brajesh!!!
    One question: is it possible to show the private message button even to non-logged in users? (Of course, the will be redirect to log-in/registration form)?

    Thanks!

  • Thanks very much! You are a Buddypress hero!

    • Thank you for the kind Words Aaron. Just doing my bits 🙂

  • Nice but… does not Work with my theme… I put all this code in Functions.php

    • Hi Salvador,
      Please contact your theme developer. They should be able to assist you better in this case.

      Regards
      Brajesh

  • thank you for this! save me hours!!!

  • Hi Brajesh,

    Cannot fill subject or title in the form for private messages, I've used &subject=COURSE or &title=COURSE and nothing happens.
    Any idea?

    Regards,
    Ale

    • Hi Ale,
      The subject and message does not work with the Button. I am not sure but it seems to me BuddyPress might not be checking the GET parameters for those fields.