Skip to content

Instantly share code, notes, and snippets.

@acousins
Created July 6, 2013 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acousins/5939925 to your computer and use it in GitHub Desktop.
Save acousins/5939925 to your computer and use it in GitHub Desktop.
add send private message button in members directory below (You can actually add it anywhere)
/**
* Get the User Id in the current context
* @param int $user_id
* @return int user_id
*/
function hibuddy_get_context_user_id($user_id=false){
if ( bp_is_my_profile() || !is_user_logged_in() )
return false;
if( !$user_id )
$user_id = bp_get_member_user_id();//for members loop
if( !$user_id && bp_is_user() ) //for user profile
$user_id = bp_displayed_user_id();
return apply_filters( 'hibuddy_get_context_user_id', $user_id );
}
/** Build the URL for sending private message**/
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 ) ) );
}
/**Generate the Send Private Message Button**/
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',
);
/**The below 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.**/
$btn = bp_get_button( $defaults );
return apply_filters( 'hibuddy_get_send_private_message_button', $btn );
}
/**Echoing the button**/
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
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 ) **/
if ( bp_is_active( 'messages' ) ){
add_action( 'bp_directory_members_actions', 'hibuddy_send_private_message_button', 30 );//experiment with the last value to change position
}
/** Source:
http://buddydev.com/buddypress/add-send-private-message-button-in-members-directory-on-a-buddypress-network/
/**
Additional:
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.
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.
.item-list .send-private-message {
margin-top: 15px;
}
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment