Adjusting the Email Subject Line in the Divi Contact Form

|

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.

This post may contain referral links which may earn a commission for this site

Divi Booster

Hundreds of new features for Divi
in one easy-to-use plugin

2 Comments

  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 *.