Skip to content

Instantly share code, notes, and snippets.

@oliwa
Last active November 27, 2018 09:34
Show Gist options
  • Save oliwa/1cbeaaa7da558d50b5bef4fd796fe925 to your computer and use it in GitHub Desktop.
Save oliwa/1cbeaaa7da558d50b5bef4fd796fe925 to your computer and use it in GitHub Desktop.
Pass a value via variable in Contact Form 7
// 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