Last active
November 27, 2018 09:34
-
-
Save oliwa/1cbeaaa7da558d50b5bef4fd796fe925 to your computer and use it in GitHub Desktop.
Pass a value via variable in Contact Form 7
This file contains 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
// see Source | |
// http://blog.ostryweb.cz/2017/05/pass-values-from-shortcode-in-contact.html | |
// in Contact Form 7 Admin | |
[text my-extra-field default:shortcode_attr readonly] | |
// in Wordpress Template | |
<?php | |
$my_extra_field = "This can be dynamic"; | |
$shortcode = '[contact-form-7 id="123" title="Name of the form" my-extra-field="'.$my_extra_field'"]'; | |
echo do_shortcode($shortcode); | |
?> | |
// in functions.php | |
<?php | |
add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 ); | |
function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) { | |
$my_attr = 'my_extra_field'; | |
if ( isset( $atts[$my_attr] ) ) { | |
$out[$my_attr] = $atts[$my_attr]; | |
} | |
return $out; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment