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, automation, 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 with robust error handling and enhanced security.

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.

Latest Posts

Fade a Divi Image Module Edge into the Background

Want to create a stylish fade effect on your Divi image module—where one side fades smoothly into the background? With a bit of CSS, you can make any edge (or corner) of the image fade out: top, bottom, left, right, or even diagonally.Fade a Divi Image Module Edge...

Hide the Header and Footer in the Hello Elementor Theme

Removing the default header and footer from your Hello Elementor theme allows for a streamlined and distraction-free website design. This is especially useful when creating unique landing pages, full-width layouts, or custom headers and footers with a page builder. In...

Setting up the Divi Password Box Module

Setting up password protection on a page can help you control access to sensitive or private content in WordPress, allowing only authorized visitors to view certain sections. With the Divi Password Box module, you can replace the plain Divi password form with a fully...

Open Divi Social Media Icons in a New Tab

Ensuring your website's social media icons open in a new browser tab creates a seamless user experience and keeps visitors engaged with your site while allowing them to explore your social media presence. This approach prevents users from navigating away from your...

Set Hover Color for Menu Links in the Divi Menu Module

Customizing the hover color of menu links lets you enhance the user experience and maintain visual consistency with your website’s brand. By setting a specific hover color, you can guide visitors' attention and improve navigation feedback. In this guide we show you...

Random Posts