Contact Form 7 Dynamic Email Field

- 5 comments

Contact Form 7,Snippets,WordPress Filters

When I develop a site, my final pre-deploy checklist includes a reminder to change any email addresses used during development to the new admin’s email because during development I like to make sure all site notices go to my test inbox instead of nuking my client’s inbox.

Contact Form 7 lets you send contact form emails to any address. By default, it will use the site admin address which it pulls in from WordPress’s admin email setting once, which means if you change the WordPress admin email after deployment then you still need to remember to change the CF7 mail to field.

I did not want this extra step so I setup a little filter which allows me to change the CF7 mail to field once when I setup the form and leave it alone.

Let’s Do It

First I change the CF7 mail to field to %admin%, then I add this filter to my theme’s functions file (usually functions.php):

Now you can remove an item from your delivery or pre-launch checklist because when you change the admin email the CF7 mail will change simultaneously.


Comments

  1. Hi Michael,
    This is about, my requirement, not sure you can help me with , but appreciate if you can have a look,

    I have configured “contact form 7” in my site, but my requirement is to be able to send the contact emails to different recipient through each post, each and every post has it’s own author that will have a separate email address and a contact us section( I have added custom field plugin so I can add a email address to each and every posts). I have tried the following guide, but it seems outdated. Can any one help me to get this requirement sorted, it is better if we can hide the email(recipient email) from the contact us section if we can.

    old link==> https://sevenspark.com/wordpress-plugins/how-to-dynamically-set-the-recipient-to-email-address-in-contact-form-7

    Thanks in advance!!

  2. Changing the To field to %admin% gives me this ‘This field has syntax errors.’ and then won’t run — How can I bypass that?

    Thanks this is really helpful.

  3. Hello,
    thanks for this very helpful code. I changed the following:

    $user_email = get_the_author_meta(‘user_email’);
    $args[‘recipient’] = str_replace(‘%admin%’, $user_email, $args[‘recipient’]);

    can u help me because this code doesnt work…
    thx manfred

Leave a Comment