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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Get the User Id in the current context * * @param int $user_id user id. * * @return int user_id */ function hibuddy_get_context_user_id( $user_id = 0 ) { if ( bp_is_my_profile() || ! is_user_logged_in() ) { return 0; } // for members loop. if ( ! $user_id ) { $user_id = bp_get_member_user_id(); } // for user profile. if ( ! $user_id && bp_is_user() ) { $user_id = bp_displayed_user_id(); } return apply_filters( 'hibuddy_get_context_user_id', $user_id ); } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function hibuddy_get_send_private_message_url() { $user_id = hibuddy_get_context_user_id(); if ( ! $user_id || $user_id == bp_loggedin_user_id() ) { return; } if ( bp_is_my_profile() || ! is_user_logged_in() ) { return false; } return apply_filters( 'hibuddy_get_send_private_message_url', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/compose/?r=' . bp_core_get_username( $user_id ) ) ); } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function hibuddy_get_send_private_message_button() { // get the user id to whom we are sending the message. $user_id = hibuddy_get_context_user_id(); // don't show the button if the user id is not present or the user id is same as logged in user id. if ( ! $user_id || $user_id == bp_loggedin_user_id() ) { return; } $defaults = array( 'id' => 'private_message-' . $user_id, 'component' => 'messages', 'must_be_logged_in' => true, 'block_self' => true, 'wrapper_id' => 'send-private-message-' . $user_id, 'wrapper_class' => 'send-private-message', 'link_href' => hibuddy_get_send_private_message_url(), 'link_title' => __( 'Send a private message to this user.', 'buddypress' ), 'link_text' => __( 'Private Message', 'buddypress' ), 'link_class' => 'send-message', ); $btn = bp_get_button( $defaults ); return apply_filters( 'hibuddy_get_send_private_message_button', $btn ); } |
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
1 2 3 | function hibuddy_send_private_message_button() { echo hibuddy_get_send_private_message_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 )
1 2 3 4 | if ( bp_is_active( 'messages' ) ) { // experiment with the last value to change position. add_action( 'bp_directory_members_actions', 'hibuddy_send_private_message_button', 30 ); } |
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.
1 2 3 | .item-list .send-private-message { margin-top: 15px; } |
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 🙂
Very cool @sbrajesh , this has been requested by so many users on buddypress.org. Thank you again for sharing an awesome solution!
Thank you Ben.
I am glad that you found it useful 🙂
Yes Brajesh, that's a very useful addition. I like it – add it's working perfect. Thanks!
Thank you Hans 🙂
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 Txema 🙂
Glad it helped.
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!
Hi,
Just posted a tutorial here
https://buddydev.com/buddypress/buddypress-private-message-non-logged-users/
Is there a way to where a visitor is not logged in can still send a message to a user? Kinda like a contact form for each member where visitors of a site can contact members without needing to registter
Hi J,
BuddyPress does not allow this functionality but we do offer a solution for this using contact form on user profile.
https://buddydev.com/plugins/buddypress-user-contact-form/
Regards
Brajesh
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!!!
I am glad it helped. Thank you for the comment.
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.