I set cookie for my users to know from which source they come to the site and I want when user contact us their message comes with their cookie as well.

So that I created a new shortcode and added in mail section but it mails direct shortcode not its returned value

Code :

function my_shortcode( $atts ) {
   return isset($_COOKIE['my_source']) ? $_COOKIE['my_source'] : '' ;
}
add_shortcode( 'my-source', 'my_shortcode' );

Message body in contact form 7 :

Name : [your-name]
Email : [your-email]
Phone : [form-tel]
My Source : [my-source]

Email I Received :

Name : Mohit Bumb
Email : abcde@gmail.com
Phone : 19191919191
My Source : [my-source]
up vote 1 down vote accepted

You should do it like so:

add_action( 'wpcf7_init', 'custom_add_form_tag_my_source' );

function custom_add_form_tag_my_source() {
  // "my-source" is the type of the form-tag
  wpcf7_add_form_tag( 'my-source', 'custom_my_source_form_tag_handler' );
}

function custom_my_source_form_tag_handler( $tag ) {
  return isset( $_COOKIE['my_source'] ) ? $_COOKIE['my_source'] : '';
}

See the documentation for more details.

Or you can also try this, to parse regular shortcodes:

add_filter( 'wpcf7_mail_components', function( $components ){
  $components['body'] = do_shortcode( $components['body'] );
  return $components;
} );
  • that is not what he asked, he asked why the email message is not parsed – lalo Dec 12 at 19:38
  • I may have forgotten about the wpcf7_special_mail_tags filter, @lalo. But the code in my answer does work - and if the question author didn't actually accept the answer, I would've probably recalled the wpcf7_special_mail_tags filter (on that day) and put it to my answer. – Sally CJ Dec 13 at 0:34

Use the filter "wpcf7_special_mail_tags"

in this example my tag is "tournaments"

/**
 * A tag to be used in "Mail" section so the user receives the special tag
 * [tournaments]
 */
add_filter('wpcf7_special_mail_tags', 'wpcf7_tag_tournament', 10, 3);
function wpcf7_tag_tournament($output, $name, $html)
{
    $name = preg_replace('/^wpcf7\./', '_', $name); // for back-compat

    $submission = WPCF7_Submission::get_instance();

    if (! $submission) {
        return $output;
    }

    if ('tournaments' == $name) {
        return $submission->get_posted_data("tournaments");
    }

    return $output;
}

I solved and posted my answer here:

Adding A Custom Form-Tag to Contact Form 7 in Wordpress

(that also works to be sent in email)

https://stackoverflow.com/questions/53754577/how-to-make-contact-form-7-custom-field/

The code

https://gist.github.com/eduardoarandah/83cad9227bc0ab13bf845ab14f2c4dad

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.