A common pattern on real estate, job board, and product sites is placing the same Contact Form 7 form on dozens of post templates — where each form submission should automatically include the ID or reference number of the specific item the visitor was enquiring about. Without this, the site owner receives form submissions with no way to trace which listing triggered the enquiry. Contact Form 7 does not natively pass page context into form fields, but there are two clean approaches: a front-end method using a hidden CF7 field populated by JavaScript, and a back-end method using the wpcf7_before_send_mail action to read an ACF field from the current post and append its value to the outgoing email. The front-end approach is visible in the form and easier to adapt for different field sources. The back-end approach is invisible to the visitor and harder to tamper with. Both are shown below.
Problem: A Contact Form 7 form appears on many different post pages. Each submission needs to include the ID or reference number of the post it was submitted from so the site owner knows which item the enquiry is about.
Solution: Either populate a hidden CF7 field via JavaScript (front-end), or hook into wpcf7_before_send_mail to read an ACF field from the current post and append it to the email body (back-end).
Method 1 — Front-end (hidden field + JavaScript). In the CF7 form builder, add a hidden field tag:
[hidden property-id id:property-id]
Then populate it from the page with jQuery before the form submits:
( function ( $ ) {
// Read the property reference from a visible element on the page
var propertyID = $( '.property-reference' ).first().text().replace( /[^0-9]/gi, '' );
$( '#property-id' ).val( propertyID );
} )( jQuery );
Method 2 — Back-end (wpcf7_before_send_mail hook). Add to functions.php. Reads the ACF field property_id from the post the form was submitted on and appends it to the email body:
<?php
add_action( 'wpcf7_before_send_mail', 'append_property_id_to_cf7_email', 10, 3 );
function append_property_id_to_cf7_email( $contact_form, &$abort, $submission ) {
// Apply only to the specific form — replace 16 with your actual form ID
if ( (int) $contact_form->id !== 16 ) {
return;
}
$post_id = (int) $submission->get_meta( 'container_post_id' );
$property_id = get_field( 'property_id', $post_id );
if ( ! $property_id ) {
return;
}
$mail = $contact_form->prop( 'mail' );
$mail['body'] .= "
Property Reference: " . sanitize_text_field( $property_id );
$contact_form->set_properties( [ 'mail' => $mail ] );
}
NOTE: The front-end method relies on a visible element being present on the page to extract the property ID from. If the ID is sensitive or must not appear in the page source, the back-end method is more appropriate — it reads directly from the database via ACF and the value never passes through the browser. For either method, confirm the CF7 form ID from the form's URL in the WordPress admin (post=16 in the edit URL).
Sources: