I love Gravity Forms! Make no mistake – there’s nothing better out there. It’s money well-spent. I recently had a client who wanted to rotate forms in his sidebar, each form sending a different PDF document. Not really a problem since Gravity Forms has a filter to attach attachments to notification emails…
add_filter("gform_user_notification_attachments", "add_attachment", 10, 3); function add_attachment($attachments, $lead, $form) { $upload = wp_upload_dir(); $upload_path = $upload["basedir"]; $attachments = array(); $attachments[] = $upload_path . '/2012/08/document_to_attach.pdf'; return $attachments; }
But it wasn’t as simple as one might expect. In this client’s case, he needed to send a different document depending on the form that was submitted. Still not a problem because Gravity Forms says you can target individual forms by adding the form ID onto the filter like this:
add_filter("gform_user_notification_attachments_37", "add_attachment_37", 10, 3); function add_attachment_37($attachments, $lead, $form) { $upload = wp_upload_dir(); $upload_path = $upload["basedir"]; $attachments = array(); $attachments[] = $upload_path . '/2012/08/document_to_attach.pdf'; return $attachments; }
In the example above, we’re targeting form ID 37 only and sending our PDF to people who fill out that form. But this still isn’t going to help my client. He needs a scenario like this for potentially 20+ forms and documents.
Logically you think, “…well add some conditional logic in there to check which form was submitted…”
Exactly. That’s what I thought too, so I did this:
add_filter("gform_user_notification_attachments", "add_attachment", 10, 3); function add_attachment($attachments, $lead, $form) { switch($form) { case 1: $doc = 'document_1.jpg'; break; case 2: $doc = 'document_2.pdf'; break; case 3: $doc = 'document_3.zip'; break; } $upload = wp_upload_dir(); $upload_path = $upload["basedir"]; $attachments = array(); $attachments[] = $upload_path . $doc; return $attachments; }
Interestingly that did not work :-O I was getting the email, but there were no attachments on any of them. Hum… If I hard-coded the $doc variable there on the line that reads $attachments[] = $upload_path . $doc;
then I would get THAT attachment. This told me there was a problem with the $form variable and that the data I was trying to compare was not what I thought it was.
Turns out I was right. A few minutes assessing the filter in the Gravity Forms source code revealed that $form was not a form ID at all, but an array. With this knowledge, I tweaked my code and got things to work.
If you have a scenario like this where you have numerous forms, all sending a different document, this solution is going to be the most efficient way to do it. Good luck!
add_filter("gform_user_notification_attachments", "add_attachment", 10, 3); function add_attachment($attachments, $lead, $form) { switch($form['id']) { case 1: $doc = 'document_1.jpg'; break; case 2: $doc = 'document_2.pdf'; break; case 3: $doc = 'document_3.pdf'; break; } $upload = wp_upload_dir(); $upload_path = $upload["basedir"]; $attachments = array(); $attachments[] = $upload_path . $doc; return $attachments; }