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.

1add_filter('wp_mail', 'dbf_contact_form_change_subject');
2
3function dbf_contact_form_change_subject($args) {
4 if (isset($_POST['et_pb_contact_subject_0'])) {
5 $subject = sanitize_text_field($_POST['et_pb_contact_subject_0']);
6 $args['subject'] = $subject;
7 }
8
9 return $args;
10}

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 From Divi Booster

Enable Automatic Looping for YouTube Videos in the Divi Video Module

Enabling automatic looping for YouTube videos in your Divi video module ensures that content plays seamlessly and continuously without interruption. This feature can enhance user engagement, highlight key information, or create dynamic visual effects on your website....

Hide Video Controls in Divi Video Module

Disabling YouTube video controls in the Divi Video Module helps create a seamless, distraction-free viewing experience for your site visitors. This approach is useful when you want full control over how your video content appears and is interacted with on the page, or...

Mute YouTube Videos by Default in the Divi Video Module

In the Divi Video Module, starting your YouTube videos muted by default can create a more user-friendly browsing experience on your webpage. It is particularly beneficial for reducing distractions, enhancing visitor engagement, and providing a seamless content preview...

Autoplay Videos in Divi Video Module

Enabling videos in your Divi video module to start playing automatically can enhance both the site's design and user engagement. To ensure full compatibility with browser requirements, such as Chrome's autoplay policy, it's often necessary to start your videos muted...

How to Add the Post Status in Divi's Dynamic Content

Divi's Dynamic Content feature is great for enhancing your post/page templates with post-specific information. While Divi includes an option to show information such as the Post Created Date using Dynamic Content, there is no equivalent option for the Post Status....

Random Divi Posts