3

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 : [email protected]
Phone : 19191919191
My Source : [my-source]

4 Answers 4

3

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;
} );
3
  • 1
    that is not what he asked, he asked why the email message is not parsed
    – lalo
    Commented Dec 12, 2018 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
    Commented Dec 13, 2018 at 0:34
  • I've tester your code and it allows to replace [my_source] only when the form is displayed, how can I use this tag is email template ?
    – Sami
    Commented Apr 1, 2020 at 22:05
1

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;
}
1

I am late but use special tag in this scenario

// Hook for additional special mail tag
add_filter( 'wpcf7_special_mail_tags', 'wti_special_mail_tag', 20, 3 );
function wti_special_mail_tag( $output, $name, $html )
{
   $name = preg_replace( '/^wpcf7\./', '_', $name );
   if ( '_my_cookie' == $name ) {
       $output = isset( $_COOKIE['my_source'] ) ? $_COOKIE['my_source'] : '';
   }
   return $output;
}

you can use [_my_cookie] to call its value

0

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 agree to our terms of service and acknowledge you have read our privacy policy.

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