Custom PM link not filling To: field

Hello,
I am currently using this code.

<?php
function ntwb_bbp_bp_pm() {
   if(function_exists('bp_send_private_message_link') && is_user_logged_in() ) {
    ?>
    <a href="<?php bp_send_private_message_link() ?>" title="Private Message" class="bbp-pm-link">Private Message</a>
    <?php
   }
}
add_action( 'bbp_theme_after_reply_author_details', 'ntwb_bbp_bp_pm' );

I have added it to a mu-plugin. This code displays a Private Message link next to a BBPress post. When clicked, you are taken to the BuddyPress Private Message form.
The problem is that the username that the link is clicked next to does not retrieve the current username, so you have to type it in manually. Please see video.
Video Link: https://drive.google.com/file/d/0B_h6oU4LzDtlcjZNNGtTZ1dGd1k/view?usp=sharing
Forum Thread Link: http://yourtechadvisors.com/forums/topic/test-topic/

Thanks.

  • Milan

    Hello @alexstine

    I hope you are well today and thanks for asking us. :slight_smile:

    Actually we will not be able to fetch username on composer new message form unless we edit core file of Buddypress. Because compose form of private message don't know from where user came to it.

    So if you want to display username on that form we will need to modify code you've written and edit core file of buddypress. if its okay to you then let me know and I will direct you further for this. :slight_smile:

    Cheers,
    Milan

  • Milan

    Hello @alexstine,

    Thanks for confirming your permission for core changes.. :slight_smile:

    After some digging with Buddypress finally I am able to achieve what you want. Please do follow below steps and take backup of your code before integrating these changes. :slight_smile:

    #1) Change your this code of "mu-plugin"

    <?php
    function ntwb_bbp_bp_pm() {
       if(function_exists('bp_send_private_message_link') && is_user_logged_in() ) {
        ?>
        <a href="<?php bp_send_private_message_link() ?>" title="Private Message" class="bbp-pm-link">Private Message</a>
        <?php
       }
    }
    add_action( 'bbp_theme_after_reply_author_details', 'ntwb_bbp_bp_pm' );

    to

    function ntwb_bbp_bp_pm() {
       if(function_exists('bp_send_private_message_link') && is_user_logged_in() ) {
    
       	//bbp_get_reply_author_id() function will give us replu author id
       	//get_userdata will return reply author's information
       	$reply_author_userData = get_userdata( bbp_get_reply_author_id() );
       	//From user_data object we are fetching only username which we require for our purpose.
       	$reply_author_username = $reply_author_userData->user_login;
    
        ?>
         <a href="<?php bp_send_private_message_link(); ?>&reply_author_username=<?php echo $reply_author_username; ?>" title="Private Message" class="bbp-pm-link">Private Message</a>
        <?php
       }
    }
    add_action( 'bbp_theme_after_reply_author_details', 'ntwb_bbp_bp_pm' );

    #2) Save mu-plugin file and now open /wp-content/plugins/buddypress/bp-templates/bp-legacy/buddypress/members/single/messages/compose.php file and go to line no. 25. You will see something like this there,

    <input type="text" name="send-to-input" class="send-to-input" id="send-to-input" />

    Replace that line with this bit of code,

    <?php
    			$reply_author_username ='';
    			if( isset( $_REQUEST['reply_author_username'] ) ){
    				$reply_author_username = $_REQUEST['reply_author_username'];
    			}
    			?>
    		<input type="text" name="send-to-input" class="send-to-input" id="send-to-input" value="<?php echo $reply_author_username; ?>" />

    #3) Save changes to file.

    That's it. :slight_smile: This will automatically fetches reply author's username and attaches it to username field of private message form.

    I hope this will help you with your requirement.

    Please let us know if there is anything else in which we can help you with. :slight_smile:

    Cheers,
    Milan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.