Skip to content

Instantly share code, notes, and snippets.

@j-falk
Last active June 18, 2022 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-falk/fc39826fea6a0691ac06d3c6c0aa8f84 to your computer and use it in GitHub Desktop.
Save j-falk/fc39826fea6a0691ac06d3c6c0aa8f84 to your computer and use it in GitHub Desktop.
Delete generated image sizes from image upload after Formidable form submission
<?php
add_action('frm_after_create_entry', 'after_entry_created', 50, 2); //use 50 to make sure this is done very last
function after_entry_created($entry_id, $form_id){
if($form_id == 8){ //change 5 to the ID of your form
$field_id = 79; //change 25 to the ID of the upload field
if(isset($_POST['item_meta'][$field_id])){
if(is_array($_POST['item_meta'][$field_id])){
foreach ($_POST['item_meta'][$field_id] as $p){
if(is_numeric($p))
$image_metadata = wp_get_attachment_metadata( $p );
$image_sizes = $image_metadata['sizes'];
$fullsizepath = get_attached_file($p);
$upload_dir = dirname( $fullsizepath ) . DIRECTORY_SEPARATOR;;
foreach ($image_sizes as $image_size) {
wp_delete_file( $upload_dir . $image_size['file'] );
}
}
}else if(is_numeric($_POST['item_meta'][$field_id])){
$image_metadata = wp_get_attachment_metadata( $_POST['item_meta'][$field_id] );
$image_sizes = $image_metadata['sizes'];
$fullsizepath = get_attached_file($_POST['item_meta'][$field_id]);
$upload_dir = dirname( $fullsizepath ) . DIRECTORY_SEPARATOR;;
foreach ($image_sizes as $image_size) {
wp_delete_file( $upload_dir . $image_size['file'] );
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment