Add a form field to the send message form AND value to be saved

I want to add a new form input to the send message form and I saw you video on how to doit BUT when I use your snippet, the value doesnt get saved so it cannot be retrieved….
I want to add a form field to the send message form but I need the value to be saved, I know how to doit in the Register form amd i works perfect but I just cannot make it work on the send message form.

add_filter(
	'hivepress/v1/forms/user_register',
	function ( $form ) {
		$form['fields'] = array_merge(
			$form['fields'],
			[
				'first_name' => [
					'required' => true,
					'_order'   => 1,
				],
			]
		);

		return $form;
	},

add_action(
	'hivepress/v1/models/user/register',
	function( $user_id, $values ) {
		if ( isset( $values['first_name'] ) ) {
			update_user_meta( $user_id, 'first_name', $values['first_name'] );
		}
	},
	10,
	2
);

Thank you

Thank you for waiting. Please try this PHP code snippet

add_filter( 'hivepress/v1/models/message', 'custom_add_message_fields', 1000 );
add_filter( 'hivepress/v1/forms/message_send', 'custom_add_message_fields', 1000);

function custom_add_message_fields($form){
	$form['fields']['custom_text_field'] = [
		'label'     => 'Custom field',
		'type'      => 'text',
		'_external' => true,
		'_order'    => 10,
	];

	return $form;
}

add_filter(
	'hivepress/v1/models/message/attributes',
	function($attributes){
		$attributes['custom_text_field'] = [
			'editable' => true,
			
			'edit_field' => [
				'label'     => 'Custom field',
				'type'      => 'text',
				'_order'    => 10,
			]
		];
		
		return $attributes;
	},
	1000
);

Thank you for the code it works saving the data but the last part that I guess adds the data to the attributes so it appears in the conversation’s attribute part doesnt work.

Or what does the last filter related to attributes does?

Thank you

This code snippet only adds the possibility to save the value of the custom field to the database. Further showing the value of the custom field requires advanced customization depending on your requirements.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.