1

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 xxxxxx@example.com by default,

[contact-form-7 id="123" title="Contact Form" destination-email="xxxxxx@example.com"]

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?

  • 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 May 2 '19 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 May 2 '19 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 May 3 '19 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 May 3 '19 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 May 3 '19 at 6:40
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="xxxxxx@example.com"]

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.

| improve this answer | |
  • 1
    Regarding your edit to the question to get this reopened - it doesn't actually change whether this is off-topic or not. I voted to approve the edit, but even with that change, it likely remains off-topic because it is specific to CF7. – butlerblog Dec 17 '19 at 15:43
  • thanks for the heads up @butlerblog, so a question like that should be asked on stackoverflow instead ? – Aurovrata Dec 17 '19 at 15:48
  • Yes - plugin specific questions are OK there (as far as I know). If this question was more generally about hooks or shortcodes, then it might be OK on WPSE, but as-is, it's too specific to CF7. – butlerblog Dec 17 '19 at 16:30
  • I see, its kind of a little confusing because there are so many questions as well meta-tags dedicated to plugins on this forum, and the majority of users just don't understand this finer point. – Aurovrata Dec 17 '19 at 16:33

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