Send Custom PDF as an Attachment

By: Barbara Smith | Asked: 04/29/2024
ForumsCategory: General questionsSend Custom PDF as an Attachment
Barbara Smith asked 2 months ago
Hoping someone here can help me as writing code is waaaaayyyy over my head.  I have multiple forms and have created views for custom PDF attachments for each form.  I want to use the custom PDF layout as the attachment PDF on the emails to both the Admin and the Applicant.  I found the below code that works for one form ID and one view ID but I have no clue how to make it work for more than one or if there is a simpler way to do this so that I don't have to edit the functions file every time I add a new form.  Here's what I'm using (I'm guessing the part that I bolded is the part that I need to adjust somehow?): // Apply a Formidable Forms view for the attached PDF in email
add_filter( 'frm_pdfs_email_attachment_args', 'add_view_to_attached_pdf', 10, 2 );
function add_view_to_attached_pdf( $pdf_args, $args ) {
if($args['form']->id ==5) { //change 5 to the ID of the form
$pdf_args['view'] = 2; // ID of view.
$pdf_args['id'] = $args['entry']->id; // Do this to show the detail view, otherwise, it shows the listing view.
return $pdf_args;
}
}
//Customize the PDF Name
add_filter( 'frm_pdfs_email_attachment_args', 'change_attached_pdf_file_name', 10, 2 );
function change_attached_pdf_file_name( $pdf_args, $args ) {
// Use the actual numeric field ID here instead of 'field_id'
$field_id = 1; // This is the ID of the field you want to include in the filename

$field_value = ''; // Default to an empty string if the field value is not found
if ( isset( $args['entry']->metas[$field_id] ) ) {
$field_value = $args['entry']->metas[$field_id]; // Fetch the field value
}
// Append the field value to the filename
$pdf_args['filename'] = 'Stuff-' . $field_value; // Change 'Stuff-' to anything you want
return $pdf_args;
} I've tried following the knowledge base guidelines but they didn't work for me at all hence the reason I'm using the above code.  Any help greatly appreciated. Thanks.
2 Answers
Best Answer
Bobby Clapp answered 2 months ago

https://connect.formidableforms.com/question/category/code-help/duplicating-a-function-for-custom-pdf-in-email

Barbara Smith replied 2 months ago

Thank you. I sorted it out finally using the below code which seems to be working for now. I appreciate you taking the time to help me.

add_filter( 'frm_pdfs_email_attachment_args', 'add_view_to_attached_pdf', 10, 2 );
function add_view_to_attached_pdf( $pdf_args, $args ) {
$args_set = false;
switch($args['form']->id) {
case 6:
$pdf_args['view'] = 554;
$args_set = true;
break;
case 7:
$pdf_args['view'] = 574;
$args_set = true;
break;
default:
break;
}
if ($args_set) {
$pdf_args['id'] = $args['entry']->id; // Do this to show the detail view, otherwise, it shows the listing view.
return $pdf_args;
}
}

Bobby Clapp answered 2 months ago

You have a couple of options if you're wanting to do this over multiple forms/views.

Learn about and implement php arrays and foreach loops: https://www.w3schools.com/php/php_looping_foreach.asp

Or have a filter for each form/view (not the non-unique function names). For example:
add_filter( 'frm_pdfs_email_attachment_args_form10_view5', 'add_view_to_attached_pdf_form10_view5', 10, 2 );
add_filter( 'frm_pdfs_email_attachment_args_form12_view8', 'add_view_to_attached_pdf_form12_view8', 10, 2 );
...

Barbara Smith replied 2 months ago

Hi Bobby - thanks for trying to help me. I changed the code to the below to where I guessed I should for my form ID's and View ID's but it's giving me this error:

Syntax error, unexpected 'add_view_to_attached_pdf_form6' (T_STRING), expecting ')'

I changed the code you gave me to:

add_filter( 'frm_pdfs_email_attachment_args_form6_view554’, 'add_view_to_attached_pdf_form6_view554’, 10, 2 );
add_filter( 'frm_pdfs_email_attachment_args_form7_view574’, 'add_view_to_attached_pdf_form7_view574’, 10, 2 );
function add_view_to_attached_pdf( $pdf_args, $args ) {
if($args['form']->id ==6) { //change 5 to the ID of the form
$pdf_args['view'] = 554; // ID of view.
$pdf_args['id'] = $args['entry']->id; // Do this to show the detail view, otherwise, it shows the listing view.
return $pdf_args;

}
}

Entirely possible I made a mistake. I was really hoping that using this plugin would mean I wouldn't have to know how to write code 🙂

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right