Dynamic Author Email with Contact Form 7

With an additional plugin and a little code, we can make dynamically addressed email forms for our site’s authors.

The Contact Form 7 Dynamic Text Extension is a handy plugin for extending Contact Form 7 functionality. With the help of this extension, we can – among other things – specify shortcode value into one of our form fields. After the form submits, we can access the shortcode field value by the ordinary CF7 way, using […] brackets and the field identifier. With this method, we can quickly pass dynamic data through our forms from our author’s meta.

Let’s see the steps!

1. Make a Standard Email Form with Contact Form 7

Install the previously mentioned two plugins (named by the CF7 and the Dynamic Text Extension) and create a new contact form with the CF7 plugin. You can add any necessary field to your form!

The only required one is a dynamic hidden type field. Add it and name it to our-custom-shortcode and at the value field add the CF7_AUTHOR_EMAIL shortcode name (we make it in the step two)!

We need the dynamic hidden field type.

At the CF7 admin, navigate to the Email tab’s “To” field and add your dynamic field: [our-custom-shortcode]. With this setting, we achieve that our contact form will send the message to our dynamically generated email address.

Setting the correct email reciever value which means our new dynamic hidden field
Note that the plugin’s validator will throw an error because the email’s syntax is not valid. It is valid we just used a dynamic field. Just in case you should make a correct self-validate: check the dynamic value in the source and send some test email! If you know what you do you can disable the validator. Add this line to your wp-config.php: define( ‘WPCF7_VALIDATE_CONFIGURATION’, false );

2. Register the Custom Shortcode to Get the Current Author’s Email

We just make a new shortcode for this job with the help of the Shortcode API! You can place the following code snippet into a new plugin or your theme functions.php:

/**
 * Get the author's email address from the author meta info.
 */

function cf7_get_author_email($atts){
    $value = '';

    if(get_the_author_meta( 'user_email' )) {
        $value = get_the_author_meta( 'user_email' );
    }

    return $value;
 }

 add_shortcode('CF7_AUTHOR_EMAIL', 'f7_get_author_email');

With this simple snippet, you get the author email address and give it back as the shortcode value.

We used the add_shortcode function, which is the default WordPress shortcode registration function. The first parameter is the name of the shortcode while the second is the used function name. We use the get_the_author_meta() function which contains the authors metadata so the author’s email.

3. Add the Form to Your author.php Template

The final step is to place the form into your author.php file. For this you can use the do_shortcode function like this:

// Display the contact form at the right place.
echo do_shortcode( '[contact-form-7 404 "Not Found"]' );
Note that WordPress, not the best at highlighting shortcodes, so it gives an error on an escaped one. You need to place here your email form’s correct code!

Summary

With these simple steps, you can easily automate the email sendings for the authors. If you run a blog or news site, this method can come handy.