BuddyDev

Search

[Resolved] Adding field to private message form? Howto do this?

  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15580

    Hello,

    I’m hoping someone can point me in the right direction to add 2 custom fields to the private message form that will also show up in the inbox areas. I can add it to html form but I need the fields to save to the db.

    Happy to fund the assistance.

  • Keymaster
    (BuddyDev Team)
    Posts: 21796
    Brajesh Singh on #15588

    This should be easy. I will ask @ravisharma to assist you with this on Monday.

    PS:- which template pack are you using? legacy or Nouveau?

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15592

    Legacy. I am using Buddyboss theme. Thanks for the response. I am looking forward to your response.

  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15637

    Any feedback on this topic?

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2459
    Ravi on #15646

    Hello studioleland,

    Check the following code in your “bp-custom.php”. It will add two fields in compose message screen and save extra field value in message meta table. On single thread screen it will show the extra field info.

    
    
    add_action( 'bp_after_messages_compose_content', function() {
    	?>
    	<select name="extra-field-1">
    		<option value="opt_1">Option 1</option>
    		<option value="opt_2">Option 2</option>
    	</select>
    	<input type="text" name="extra-field-2">
    	<?php
    } );
    
    add_action( 'messages_message_sent', function ( $message ) {
    
    	if ( ! isset( $_POST['meta'] ) ) {
    		return;
    	}
    
    	if ( isset( $_POST['meta']['extra-field-1'] ) ) {
    		bp_messages_update_meta( $message->id, '_extra_field_1', $_POST['meta']['extra-field-1'] );
    	}
    
    	if ( isset( $_POST['meta']['extra-field-2'] ) ) {
    		bp_messages_update_meta( $message->id, '_extra_field_2', $_POST['meta']['extra-field-2'] );
    	}
    } );
    
    add_filter( 'bp_get_the_thread_message_content', function( $content ) {
    	global $thread_template;
    	$message_id = $thread_template->message->id;
    	$append_content = 'Field-1: ' . bp_messages_get_meta( $message_id, '_extra_field_1', true ) . PHP_EOL;
    	$append_content .= 'Field-2: ' . bp_messages_get_meta( $message_id, '_extra_field_2', true );
    	return $content . $append_content;
    } );
    
    

    Thank You
    Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2459
    Ravi on #15647

    Hello Studioleland,

    I have posted above code will work with bp-nouveau template. Please use the following section for bp-legacy templates.

    
    add_action( 'messages_message_sent', function ( $message ) {
    
    	if ( isset( $_POST['extra-field-1'] ) ) {
    		bp_messages_update_meta( $message->id, '_extra_field_1', $_POST['extra-field-1'] );
    	}
    
    	if ( isset( $_POST['extra-field-2'] ) ) {
    		bp_messages_update_meta( $message->id, '_extra_field_2', $_POST['extra-field-2'] );
    	}
    } );
    
    

    Thanks
    Ravi

  • Participant
    Level: Enlightened
    Posts: 27
    studioleland on #15648

    This is fantastic! Thanks very much. I’ll post my solution shortly. No doubt others will benefit since I couldn’t find anything on the core BP site for this.

  • Participant
    Level: Initiated
    Posts: 2
    Florent Maillefaud on #30896

    Hi! Thanks for this code.

    But i’m try to add a FILE input : <input type=”file” name=”extra-field-file” id=”extra-field-file” />

    But it didn’t work.. can you help me ?

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 21796
    Brajesh Singh on #30913

    Hi Florent,
    Thank you for the question.

    handling file uploads are different than the normal fields.

    I will suggest to use a dedicated plugin like BuddyPress message attachment for your need(or look inot that and see if that suits you)

    https://wordpress.org/plugins/buddypress-message-attachment/

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 2
    Florent Maillefaud on #30917

    Sorry, i try this by it don’t working..

    A problem with AJAX script, the page is reloading every time when i click for download file. After, reloaded, it’s wordking..

    Thanks for your help

You must be logged in to reply to this topic.

This topic is: resolved