Description
The gravityflowpdf_generated action allows customization to occur immediately after the successful generation of the PDF file on the server.
Parameters
Parameter | Type | Definition |
---|---|---|
$file_name | String | The file name of the newly generated PDF file. |
$entry_id | Integer | The entry ID for which the PDF step was processed. |
$form_id | Integer | The form ID for which the entry was submitted. |
Examples
Create a copy of the PDF file with a custom name in the same folder
1 2 3 4 5 6 7 8 | add_action( 'gravityflowpdf_generated' , 'sh_gravityflowpdf_generated' , 10, 3 ); function sh_gravityflowpdf_generated( $file_name , $entry_id , $form_id ) { $copied_file_name = 'my-file-abcd.pdf' ; $file_path = gravity_flow_pdf()->get_destination_folder(); $source_file_path = gravity_flow_pdf()->get_file_path( $entry_id , $form_id ); $target_file_path = $file_path . $copied_file_name ; copy ( $source_file_path , $target_file_path ); } |
Create a copy of the PDF file with step/entry specific filename in a different folder.
Store the URL of new file in a single file upload field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | //Store a Gravity Flow PDF generated PDF into a separate path in wp-content and store the URL to it in a form field. add_action( 'gravityflowpdf_generated' , 'jo_gravityflowpdf_generated' , 10, 3 ); function jo_gravityflowpdf_generated( $file_name , $entry_id , $form_id ) { $api = new Gravity_Flow_API( $form_id ); $entry = GFAPI::get_entry( $entry_id ); $step = $api ->get_current_step( $entry ); gravity_flow()->log_debug( 'Storing file for Entry #' . $entry_id . ' on Step #' . $step ->get_id() ); //Use different fields and filenames for each step that is generating a PDF. switch ( $step ->get_id() ) { //Create a separate case block for each step that you need to store PDF file from. case '2312' : //Define a unique name for the new file. //This example uses a hard-coded start (example), current date, simplified field value, entry ID and random number. $partial_filename = str_replace ( ' ' , '' , preg_replace( "/[^A-Za-z0-9]/" , '' , $entry [ '3' ] ) ); $new_filename = sprintf( 'example-%s-%s-%s-%s.pdf' , date ( "ymd" ), $partial_filename , $entry_id , rand(100000, 750000) ); $copied_file_url = content_url() . '/uploads/gravityflowpdf/' . $new_filename ; //What field ID will the URL of the file stored into? $stored_field_id = '7' ; break ; } gravity_flow()->log_debug( 'gravityflowpdf_generated - Storing file into ' . $copied_file_url ); //Define path to store file and copy it there. $file_path = gravity_flow_pdf()->get_destination_folder(); $source_file_path = gravity_flow_pdf()->get_file_path( $entry_id , $form_id ); $target_file_path = $file_path . $new_filename ; copy ( $source_file_path , $target_file_path ); gravity_flow()->log_debug( 'gravityflowpdf_generated - Updating file upload field #' . $stored_field_id . ' with the new file location' ); GFAPI::update_entry_field( $entry_id , $stored_field_id , $copied_file_url ); } |
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?