The task was to pass the values from the Wordpress template script to Contact Form 7 form. PHP template script creates the correct Contact Form 7 shortcode with generated values and the result is form with pre filled fields with these values. In my case I then used CSS styling to make the fields look like regular text, as Contact Form 7 has no function to simply output the values without creating the form fields (AFAIK).
In the Wordpress Contact Form 7 admin - create a form and add fields like this (I'm adding more then one as an example). The default:shortcode_attr parameter means that the prefilled value is expected to be passed from the shortcode. You have also other options, which can be multiple and are processed one after another until the value is passed (see this for details). I'm also adding readonly parameter as I will use the value as regular text, not real input field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[text field-one default:shortcode_attr readonly] | |
[text field-two default:shortcode_attr readonly] |
In Wordpress template I generate the form:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$field-one = "This can be dynamic"; | |
$field-two = "This can be also dynamic"; | |
$shortcode = '[contact-form-7 id="123" title="Name of the form" field-one="'.$field-one.'" field-two="'.$field-two.'"]'; | |
echo do_shortcode($shortcode); | |
?> |
And to be able to pass the values to these custom variables, also this piece of code has to be added to your Wordpress template's functions.php file. You need to change the names of the fields to match both form in admin and shortcode in template script:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 ); | |
function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) { | |
$my_attr = 'field-one'; | |
if ( isset( $atts[$my_attr] ) ) { | |
$out[$my_attr] = $atts[$my_attr]; | |
} | |
$my_attr = 'field-two'; | |
if ( isset( $atts[$my_attr] ) ) { | |
$out[$my_attr] = $atts[$my_attr]; | |
} | |
return $out; | |
} | |
?> |
The resulting CF7 form will have two fields (here field-one and field-two) pre populated with the text "This can be dynamic" and "This can be also dynamic".
Thank you! With your article I was able to really understand how it works to pass parameters via shortcode.
OdpovědětVymazatFor me also working fine. Thanks a lot!
OdpovědětVymazat