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;
}
Related Post: Adding PHP to the Divi Theme
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.
Hi Dan,
Does this also work if you use an smtp plugin?
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!
Thanks for the fast reply and info, I'll give it a try!
You’re welcome, Nathan. Best of luck with it!
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.
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!