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

How to set Contact Form 7 fields default value using shortcode attribute?

programmeradmin5浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

The contact form 7 plugin provides an option to set the default value of a form field using a shortcode attribute (as documented here). This can be useful to dynamically change the recipient of a notification email using say a destination-email field tag in a form and use a shortcode attribute to set its default value dynamically from within a page template using the do_shortcode function. So for example, the following shortcode would send the email to [email protected] by default,

[contact-form-7 id="123" title="Contact Form" destination-email="[email protected]"]

However, it is not clear how to get it to work.

I already read other post where they were asking the same:

ACF + contact form 7

I must say that the solution didn't work for me. The contact-form keeps sending the emails to the email address set on Contact Form 7 configuration.

This is what i put on my single.php file:

<?php echo do_shortcode( '[contact-form-7 id="5" title="Form World Unite" destination-email="'.get_field( 'email' ).'"]' ); ?>

What could be wrong?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

The contact form 7 plugin provides an option to set the default value of a form field using a shortcode attribute (as documented here). This can be useful to dynamically change the recipient of a notification email using say a destination-email field tag in a form and use a shortcode attribute to set its default value dynamically from within a page template using the do_shortcode function. So for example, the following shortcode would send the email to [email protected] by default,

[contact-form-7 id="123" title="Contact Form" destination-email="[email protected]"]

However, it is not clear how to get it to work.

I already read other post where they were asking the same:

ACF + contact form 7

I must say that the solution didn't work for me. The contact-form keeps sending the emails to the email address set on Contact Form 7 configuration.

This is what i put on my single.php file:

<?php echo do_shortcode( '[contact-form-7 id="5" title="Form World Unite" destination-email="'.get_field( 'email' ).'"]' ); ?>

What could be wrong?

Share Improve this question edited Dec 17, 2019 at 15:46 Aurovrata 1,34611 silver badges18 bronze badges asked May 2, 2019 at 12:07 Draws Ren GundamDraws Ren Gundam 251 gold badge2 silver badges6 bronze badges 5
  • From a quick skip of the plugin code it might be recipient= not destination-email=. Or maybe mail=. But I'm not sure sorry. – Rup Commented May 2, 2019 at 13:05
  • Why don't you create a second form in the backend with a different recipient? Put the second shortcode in your template - done. – user3135691 Commented May 2, 2019 at 14:12
  • The idea is having a different contact form for every author who logs in and create a post. So i can't do that. – Draws Ren Gundam Commented May 3, 2019 at 3:55
  • I haven't seen anything in the plugin code (Contact Form 7) that allows to set the recipient in the shortcode itself. What I would do instead is to hook the data when the form is sent and change the recipient on the fly based on the currently logged in user. – Mike Commented May 3, 2019 at 6:38
  • I found another solution, just use the Contact Form 7 Custom Recipient plugin.With this plugin i can change the recipient everytime i create a post, that works for the moment. – Draws Ren Gundam Commented May 3, 2019 at 6:40
Add a comment  | 

1 Answer 1

Reset to default 2

the destination-email attribute requires an additional filter to be hooked to work, as detailed in cf7 doc and this forum thread, you need to ensure you do the following 4 steps:

1) in addition to the short code attribute,

[contact-form-7 id="123" title="Contact Form" destination-email="[email protected]"]

2) you need to,

add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 );
function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
    $my_attr = 'destination-email';
    if ( isset( $atts[$my_attr] ) ) {
        $out[$my_attr] = $atts[$my_attr];
    }
    return $out;
}`

3) which assumes you have a field in your form

[email* destination-email default:shortcode_attr]
//If you want this field hidden you can use this code instead:
[hidden destination-email default:shortcode_attr]

4) and last but not least you need to use the [destination-email] mail tag in the 'To:' field in the mail configuration tab.

发布评论

评论列表(0)

  1. 暂无评论