最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Add custom variable in Contact Form 7 mail body

programmeradmin5浏览0评论

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]

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]
Share Improve this question asked Nov 30, 2018 at 10:25 Mohit BumbMohit Bumb 3751 gold badge6 silver badges19 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 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;
} );

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 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

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/questions/53754577/how-to-make-contact-form-7-custom-field/

The code

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

发布评论

评论列表(0)

  1. 暂无评论