質問

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]
役に立ちましたか?

解決

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

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

ライセンス: CC-BY-SA帰属
所属していません wordpress.stackexchange
scroll top