That sounds about right, since ACF uses their custom “get_field” or “the_field” to format metadata based on your settings before returning the output despite the raw data being the attachment id, whereas DTX just gets and returns whatever the metadata is there.
I recommend using using our wpcf7dtx_shortcode
filter to format it. You can do so like this:
/**
* Get formatted ACF value
*
* @param string $value The sanitized & escaped value that is being outputted by DTX, possibly obfuscated depending on settings.
* @param mixed $raw_value The raw value retrieved by the built-in shortcode, or the default value depending on settings.
* @param string $tag Shortcode tag that was used, without the "CF7_" prefix
* @param array $atts Associative array of the entire shortcode's attributes, combined and filtered with user input.
*
* @return string the modified value
*/
function kapuzestattmuetze_acf_cf7($value, $raw_value, $tag, $atts)
{
// Do nothing for all other CF7 fields or if ACF is not available
if($tag != 'get_custom_field' || !array_key_exists('key', $atts) || $atts['key'] != 'my_file_acf_field_name' || !function_exists('get_field')) return $value;
return esc_url(get_field($raw_value));
}
add_filter('wpcf7dtx_shortcode', 'kapuzestattmuetze_acf_cf7', 10, 4);
Then in your form template, you’d use this form tag to reveal the URL in the form.
[dynamic_hidden post_pdf "CF7_get_custom_field key='my_file_acf_field_name'"]
Then in your mail template, using this shortcode to display the pdf URL submitted in your form would be as easy as this:
<a href="[post_pdf]" target="_blank" rel="noopener noreferrer nofollow">Click Here</a>
Note: you can use whatever for your field name, I was using post_pdf
as an example because you did not mention what yours was called. I also used my_file_acf_field_name
for example purposes since that info is also not known to me.
——————————————————————————————-
Alternatively, if your plan is to provide a PDF through a form that acts like gated content, I recommend using the wpcf7_before_send_mail
action hook to retrieve the file. That way, people like me don’t go looking at your source code where the form is located and finding the free resource tucked away in a hidden field without submitting info ??
So instead of anything I said above, use this code snippet instead:
/**
* Custom Email Behavior on Form Submission
*
* Add gated content (ACF file PDF associated with post) to email
*
* @param WPCF7_ContactForm $contact_form The current contact form.
* @param bool $abort If the submission is being aborted.
* @param WPCF7_Submission $submission The current submission data.
*
* @return void
*/
function kapuzestattmuetze_acf_cf7_pdf($contact_form, &$abort, $submission)
{
// Identify the post this form was submitted on (sanitize & validate)
$post_id = intval(sanitize_text_field($submission->get_meta('container_post_id')));
if ($post_id && function_exists('get_field')) {
// Get the ACF PDF URL associated with that post (sanitize & validate)
$pdf_url = sanitize_url(get_field('my_file_acf_field_name', $post_id));
if ($pdf_url) {
// Update email bodies with the URL
$mail = $contact_form->prop('mail'); // Get the first mail property
if ($mail['active']) {
// If the first email is active, replace the shortcode with the value in the body of the email
$mail['body'] = str_replace('[pdf_url]', $pdf_url, $mail['body']);
$mail['use_html'] = true; // Force the email to encode HTML so the list appears correctly
}
$mail2 = $contact_form->prop('mail_2'); // Get the second mail property
if ($mail2['active']) {
// If the second email is active, replace the shortcode with the value
$mail2['body'] = str_replace('[pdf_url]', $pdf_url, $mail2['body']);
$mail2['use_html'] = true; // Force the email to encode HTML so the list appears correctly
}
// Update the form with the updated values
$contact_form->set_properties(array(
'mail' => $mail,
'mail_2' => $mail2
));
}
}
}
add_action('wpcf7_before_send_mail', 'kapuzestattmuetze_acf_cf7_pdf', 10, 3);
Don’t add anything related to the PDF in your form template, since the post id is captured automatically with every submission. Then in your mail template, go ahead and keep the shortcode there. The function in the snippet above manually replaces it before the emails go out.