Email post author with Fluent Forms

tdrayson

A user in the Fluent Forms Facebook group wanted to email the author of the post that the form was submitted on.

Fluent Forms has a built in smartcode that you can set as the default value when the page loads, however this exposes the authors email in the source code.

Another way we can achieve the same outcome is by using the insert_response_data hook and insert the author email after the form has been saved to the database, but before the email confirmations are run.

1. Add your fields

We need to have two fields in our form.

  • An empty email field with the name attribute author_email
  • A hidden field with the name attribute post_id

We need to set the default value of the hidden field to the post ID using the smartcode {embed_post.ID}

To hide our author email field on the frontend to users, we can add the class ff-hidden to our field container.

screenshot 2023 03 06 at 10.12.27

2. Setting up email confirmation

Next we need to setup our email confirmation in the form settings.

For the Send to field, we need to select our author email field that we just created.

3. Inserting the author email after submission

Finally we need to add the code to insert the author email. Add the code below to functions.php or code snippet plugin.

Make sure you update line 4 with the correct ID for your form.


        add_filter("fluentform_insert_response_data", "tct_email_author", 10, 3);

function tct_email_author($formData, $formId, $inputConfigs){
    if ($formId != 1) { // Replace with your Form ID
        return $formData;
    }
    
    $post_id = $formData["post_id"];
    $post = get_post($post_id);
    $author_id = $post->post_author;
    
    $formData["author_email"] = get_the_author_meta("user_email", $author_id);

    return $formData;
}

      
Copy

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise