I'm using Contact Fom 7 and need to add a custom text somewhere to the form.
I'm getting the custom text with PHP from a subfield of Advanced Custom Fields. I know, that there is an extra Plugin called "Contact Form 7 Dynamic Text Extension" for that (https://de.wordpress.org/plugins/contact-form-7-dynamic-text-extension/). But because the text is in a subfield, I cant use it.
So, what I need is a text in an hidden input field or in the generated e-mail of the form.
What I thought was a custom parameter in the CF7 shortcode itself. Like this:
[contact-form-7 id="1" title="Title" customtext="Text"]
Is that possible?
Or is it possible to use the title and add it to an input field or in the e-mail of the form?
you have a field named “customtext” for the destination email address:
[text* customtext]
To get the default value from shortcode attributes, add the default:shortcode_attr option to the form-tag:
[text* customtext default:shortcode_attr]
Then, add an attribute with the same name as the field (“customtext” in this case) into the shortcode for the contact form:
[contact-form-7 id="123" title="Contact Form" customtext="xxxxxx@example.com"]
you need to register the attribute beforehand.
Add the following code snippet to your theme’s functions.php file:
add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 );
function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
$my_attr = 'customtext;
if ( isset( $atts[$my_attr] ) ) {
$out[$my_attr] = $atts[$my_attr];
}
return $out;
}
Referance link : https://contactform7.com/getting-default-values-from-shortcode-attributes/