Adjusting the Email Subject Line in the Divi Contact Form

Written by Dan Mossop

For those using the Divi Contact Form module, you may want to adjust the subject line of the email the form sends to something more useful that the default. Here's how to do so.

Use the Divi Contact Form Subject Field as the Email Subject Line

The following PHP code allows you to set the subject line as the content entered by your site visitor in the contact form's subject field.

add_filter('wp_mail', 'dbf_contact_form_change_subject');

function dbf_contact_form_change_subject($args) {
    if (isset($_POST['et_pb_contact_subject_0'])) { 
        $subject = sanitize_text_field($_POST['et_pb_contact_subject_0']);
        $args['subject'] = $subject;
    }

    return $args;
}

First, we attach our function to the 'wp_mail' filter which is applied just before any email is sent off by WordPress, and hence, our Divi contact form. The function takes the arguments provided by the 'wp_mail' filter and checks if the 'et_pb_contact_subject_0' field is set in the global $_POST array. This field corresponds to the subject field in our Divi contact form. We then set this sanitized version of our $_POST['et_pb_contact_subject_0'] value as the new subject of the email to be sent and return the result.

Run PHP Code Directly in your Divi Layouts

Unlock endless customization and dynamic functionality by seamlessly adding PHP code to your Divi pages and posts with the Divi PHP Code Module. Style, preview, and debug your PHP creations directly in the visual builder, making it perfect for adjusting elements like contact form emails.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

6 Comments

    • Hey Nathan,

      It should do, yes. This code modifies the subject line of the email before WordPress tries to send it, while an SMTP plugin modifies how the email is actually sent, so they shouldn’t interfere with one another.

      That said, some SMTP plugins might offer additional features that allow modifying the email subject line and then there’s a chance that either this code or the plugin feature doesn’t take effect.

      I’d suggest giving it a go and if you do have any problems with it let me know which SMTP plugin you’re using so that I can check it out.

      I hope that helps!

      Reply
      • Thanks for the fast reply and info, I'll give it a try!

        Reply
        • You’re welcome, Nathan. Best of luck with it!

          Reply
  1. Unfortunate that, after all these years of Divi, PHP is required for this simple change. I mean, even Avada has this option. With the focus of Elegant Themes on new paid offerings to 'better' Divi, it shows dark clouds on the horizon for the user base.

    Reply
    • Hey Asad, yeah, it would be nice if this was included in the Contact Form module – I think it would be a common enough requirement to warrant it. I believe at the moment most of Elegant Themes' efforts are going into the Divi 5 update, and these new paid offerings are a way to enhance Divi without needing to change Divi itself too much (as changes they make to Divi now would need to be redone to fit in with Divi 5). Hopefully once Divi 5 is out, they'll be able to turn their attention back to extending the options available in Divi and the built-in modules. Cheers!

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

Your email address will not be published. Required fields are marked *.

We may earn a commission when you visit links on our website.