BuddyDev

Search

[Resolved] Add Send Private Message Button on Friend's section in every user profile

  • Participant
    Level: Enlightened
    Posts: 47
    Sotiris on #30792

    I tried to use the code from the link https://buddydev.com/add-send-private-message-button-in-members-directory-on-a-buddypress-network/ but the page kept freezing. I finally wrote the code but I can not show app the button to the Friend’s section in every user profile

  • Keymaster
    (BuddyDev Team)
    Posts: 24315
    Brajesh Singh on #30793

    Please share more details.

    Which theme are you using? Which template pack and by freezing, did you mean wsod or something else?

    Please let me know and I will assist.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 47
    Sotiris on #30799

    Apologies. The freezing is about the buddydev.com page.

    The theme I use on the site is the Community Builder Pro Child and the template pack is the “legacy”.
    The code I have used is shown below.

    
    /**
     * Get a link to send a BetterMessage PM to the given User.
     *
     * @param int $user_id user id.
     *
     * @return string
     */
    function bm_get_send_private_message_to_user_url( $user_id ) {
        return bp_loggedin_user_domain() . 'bp-messages/?new-message&fast=1&to=' . bp_core_get_username( $user_id );
    }
    /********************
    //Show private message button on members list
    ********************/
    /**
     * 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
       return false;
     //$user_id = bp_displayed_user_id();
    
     return apply_filters( 'hibuddy_get_context_user_id', $user_id );
    }
    
    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();	
    
    	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' => bm_get_send_private_message_to_user_url($user_id),
    	'link_title' => __('Send a private message to this user.', 'buddypress'),
    	'link_rel' => __('Private Message', 'buddypress'),
    	'link_class' => 'send-message'
    	);
    
    	$btn = bp_get_button( $defaults );
    
    	return apply_filters('hibuddy_get_send_private_message_button', $btn);
    }
    
    function hibuddy_send_private_message_button() {
      $user_id = hibuddy_get_context_user_id();
      //echo buddydev_private_message_button_shortcode( $user_id );
      echo hibuddy_get_send_private_message_button();
    }
    
    if ( bp_is_active( 'messages' ) ){
    	add_action( 'bp_directory_members_actions', 'hibuddy_send_private_message_button', 10 );
      	//experiment with the last value to change position
    }
    
  • Keymaster
    (BuddyDev Team)
    Posts: 24315
    Brajesh Singh on #30806

    Thank you.

    Please change this

    
    'block_self' => true
    

    to

    
    
    'block_self' => false
    
    

    That will make it appear in friends.

    I also suggest adding link text such as

    
     'link_text' => __('Send a private message.', 'buddypress'),
    

    to make link text appear correctly.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 47
    Sotiris on #30814

    Thank you, for your help Brajesh.

    Along with the change in function below (Removed the bp_is_my_profile() check), did the trick!

    
    function hibuddy_get_context_user_id($user_id=false){
    
    if ( !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
       return false;
     //$user_id = bp_displayed_user_id();
    
     return apply_filters( 'hibuddy_get_context_user_id', $user_id );
    }
    

    Regards,
    Sotiris

  • Keymaster
    (BuddyDev Team)
    Posts: 24315
    Brajesh Singh on #30828

    You are welcome.
    I am glad it is working now.

    Regards
    Brajesh

The topic ‘ [Resolved] Add Send Private Message Button on Friend's section in every user profile’ is closed to new replies.

This topic is: resolved