With GravityPDF is there a way to automatically add a PDF to every form created by default, rather than having to manually add a PDF each time?
Just in case anyone has the need to do the same in future, the following snippet will auto add PDF’s to existing and new Gravity Forms.
add_action( 'init', function() {
if ( class_exists( 'GPDFAPI' ) ) {
// change $settings to reflect your Form Name and the template you're using
$settings = array(
'name' => 'Form Name',
'template' => 'template-name',
'filename' => 'File-Name',
);
// Gets all Gravity Forms
$forms = GFAPI::get_forms();
// Loops through each Gravity Form
foreach($forms as $form) {
// $pdfexists = 0 each loop
$pdfexists = 0;
// Gets Gravity Form Id
$form_id = rgar( $form, 'id' );
// Gets all PDFS for the Gravity Form
$pdfs = GPDFAPI::get_form_pdfs( $form_id );
foreach ( $pdfs as $pdf ) {
// Loops through all PDFS and if PDF already exists then exits loop
if ( $pdf['name'] === $settings['name'] ) { $pdfexists = 1; break 1;}
}
// If the PDF does not already exist, add PDF
if ($pdfexists != 1) { $pdf = GPDFAPI::add_pdf( $form_id, $settings ); }
}
}
});
1 Like