Add template text to the start or end of the contact form's subject

Sometimes you might want to add something to the beginning or end of your contact form subject line in order to create rules or filters in your email or help desk. This text is added to the beginning or end of the subject line after a reader clicks submit, so there's no chance that they could delete it or change it. They never know it's there, and you can set up email or help desk queue rules and filters to help process these submissions.

We like to use a code snippet added to the the Custom HTML to solve this problem. To set it up:

  1. Go to Library > Snippets.
  2. Click the  + Create New Snippet button.
    Click the Create New Snippet button
  3. Give your snippet a Name. This is how the snippet will appear in the Snippet Library and in snippet look-ups. The Merge Code Name will auto-generate from the Snippet Name, but you can edit it if you'd like to. For example: Contact Form Subject.
  4. Give your snippet a Description. This will help describe what your snippet does to other users (and may help remind you if you forget!) For example: Add "Knowledge Base Request: " to the start of all contact form submissions.
  5.  Click the dropdown next to Snippet Content to select the Code Editor.
    Select Code Editor from the Snippet Content dropdown
  6. Copy the code below and paste it into the Code Editor. The code below will add "Knowledge Base Request: " to the subject line of all contact form submissions. You can edit that text to be what you'd like to use.
    <script>
      //add "Knowledge Base Request: " to subject line
      $('.hg-contact-form-container  button[type=submit]').click(function(e){
        e.preventDefault();
        var subject = $('.hg-contact-form-container input[name=subject]').val();
        $('.hg-contact-form-container input[name=subject]').val("Knowledge Base Request: " + subject);
        $('.hg-contact-us-form form').submit();
      });
    </script>
  7. Here's what a finished snippet might look like:
    Sample snippet using the code above
  8. Click the Create button.
  9. Copy the snippet's Merge Code Value. (In our example, it's {{snippet.contactFormSubject}}).
  10. Go to Settings > Style.
  11. Click the Custom HTML tab below the preview pane.
  12. Be sure the dropdown is set to Body.
  13. Paste your merge code anywhere into the Body.
    Sample Custom HTML > Body with snippet added
  14. Save your changes.

Customize the subject line order

The code sample will put the text you have into the start of the contact form's subject. If you'd rather put it into the end, adjust the script so that subject + comes before the text string, for example:

<script>
  //add "Knowledge Base Request: " to subject line
  $('.hg-contact-form-container  button[type=submit]').click(function(e){
    e.preventDefault();
    var subject = $('.hg-contact-form-container input[name=subject]').val();
    $('.hg-contact-form-container input[name=subject]').val(subject + " - Knowledge Base Request");
    $('.hg-contact-us-form form').submit();
  });
</script>