Gravity Forms add attachment to user notification

Tags: , , , ,



I am using the following code from Gravity forms to attach a file to the user’s notification:

add_filter( 'gform_notification_55', 'add_attachment_pdf', 10, 3 ); //target form id 2, change to your form id
function add_attachment_pdf( $notification, $form, $entry ) {
    //There is no concept of user notifications anymore, so we will need to target notifications based on other criteria,
    //such as name or subject
    if( $notification['name'] == 'User Notification' ) {
        //get upload root for WordPress
        $upload = wp_upload_dir();
        $upload_path = $upload['basedir'];

        //add file, use full path , example -- $attachment = "C:\xampp\htdocs\wpdev\wp-content\uploads\test.txt"
        $attachment = $upload_path . '/2020-RSPA-POS-Channel-KPI-Study-Update-Post-COVID.pdf';

        GFCommon::log_debug( __METHOD__ . '(): file to be attached: ' . $attachment );


    }
    //return altered notification object
    return $notification;
}

In addition to adding an attachment to form 55, as shown in the example, I need to add a different attachment to a different form. I have changed the form ID to reflect this and copied/pasted the code into the functions file a second time, but the site breaks in doing so. The above works as expected, on it’s own. How can I use the filter multiple times?

Answer

The absolute simplest way (but not the prettiest) would be to duplicate everything and change the function name. Here’s an example where I update the function name in both instances to match the form ID:

add_filter( 'gform_notification_55', 'add_attachment_pdf_55', 10, 3 );
function add_attachment_pdf_55( $notification, $form, $entry ) {
    ...
}

add_filter( 'gform_notification_56', 'add_attachment_pdf_56', 10, 3 );
function add_attachment_pdf_56( $notification, $form, $entry ) {
    ...
}


Source: stackoverflow