Adding dynamic data to Contact Form 7

You or your customer has real estate a website. the website has many posts that description real estate and each post has contact form based Contact Form 7. The objective is to add real estate ID to this contact form. This step need for identification real estate from where, user formed a request. Doesn’t matter how created ID, using ID of post or use ACF. In our case ID creating use ACF field.

How to add dynamic data to Contact Form 7

Problem: How to add dynamic data (ID) to Contact Form 7

Solution: Well, traditionally we'll review 2 ways for resolve this task.

Front-end way. Firstly we should be add hidden field to Contact Form 7 use next string:

[hidden property-id id:property-id]

After then we write function use jQuery:


function addPropertyID() {
   if ( $( '.property-template-default' ).length > 0 ) {
      var propertyID = $( '' ).text().replace( /[^0-9]/gi, '' );
      $( '#property-id' ).val( propertyID );   

Back-end way. In this case, we should be add string e.g. 'Property ID:' to message in Contact Form 7, that user gets after send form. Add next code to your function.php file:

// Add Property ID To Email Message
add_action( 'wpcf7_before_send_mail', 'add_property_ID', 10, 3 );

function add_property_ID( $contact_form, &$abort, $submission ) {
	$form_id = $contact_form->id;
    // Use function only our form, that has ID = 16
	if ( $form_id == 16 ) {
        // Get Post ID
		$post_id = $submission->get_meta( 'container_post_id' );
        // Get ACF field contain		
        $property_id = get_field( 'property_id', $post_id );
		$mail = $contact_form->prop( 'mail' );
        // Add Property ID
		$mail['body'] .= 'Property ID: ' . $property_id;
		$contact_form->set_properties( [ 'mail' => $mail ] );



Leave Comment

One Reply to “How to add dynamic data to Contact Form 7”

Your email address will not be published. Required fields are marked *