frm_before_destroy_entry

Back to Top
Knowledge BaseEntry Management → frm_before_destroy_entry

This hook allows you to do something before an entry is destroyed.

Usage

add_action('frm_before_destroy_entry', 'my_custom_function');

Parameters

  • $entry_id (integer)

Examples

Formidable Hook - Helpful

Basic Example

Submitted by  — 8 years ago

add_action('frm_before_destroy_entry', 'my_custom_function');
function my_custom_function($entry_id) {
  $entry = FrmEntry::getOne($entry_id);
  if ( $entry->form_id == 5 ) { // change 5 to your form id
    // do something for entries deleted from this form
  }
}
Formidable Hook - Helpful

Delete Uploads

Submitted by  — 8 years ago

As of v2.0.22, this is not longer needed. You can instead check the box in the field options for your file upload field.
Use this code to automatically delete file uploads when an entry is deleted. No changes are necessary.

add_action('frm_before_destroy_entry', 'delete_entry_uploads', 10, 2);
function delete_entry_uploads( $entry_id, $entry = false ) {
  if ( empty( $entry ) ) {
    $entry = FrmEntry::getOne( $entry_id );
  }
  if ( empty( $entry ) ) {
    // If there is no entry, don't continue checking for files
    return;
  }
  $upload_fields = FrmField::getAll(array('fi.type' => 'file', 'fi.form_id' => $entry->form_id));
  foreach ( $upload_fields as $upload_field ) {
    $media_id = FrmEntryMeta::get_entry_meta_by_field($entry->id, $upload_field->id);
    if ( !$media_id && $entry->post_id ) { //if this is a post field, get the value
      if ( !isset($upload_field->field_options['post_field']) ) {
        $upload_field->field_options['post_field'] = '';
      }
      if ( !isset($upload_field->field_options['custom_field']) ) {
        $upload_field->field_options['custom_field'] = '';
      }
      if ( $upload_field->field_options['post_field'] ) {
        $media_id = FrmProEntryMetaHelper::get_post_value(
          $entry->post_id, $upload_field->field_options['post_field'],
          $upload_field->field_options['custom_field'], array('type' => 'file')
        );
      }
    }
    
    if ( !$media_id ) {
      continue;
    }
    $media_id = maybe_unserialize($media_id);
    if ( is_numeric($media_id) ) {
      wp_delete_attachment($media_id, true); 
    } else if ( is_array($media_id) ) {
      foreach ( $media_id as $m ) {
        if ( is_numeric($m) ) {
          wp_delete_attachment($m, true);
        }
      }
    }
    unset($upload_field, $media_id);
  }
}
Formidable Hook - Helpful

Separate child entries

Submitted by  — 8 years ago

Use the code below to prevent entries from embedded forms and repeating sections from being deleted when the parent entry is deleted. This will automatically apply to all embedded forms and repeating sections in the form.

add_action( 'frm_before_destroy_entry', 'my_custom_function', 9 );
function my_custom_function( $entry_id ) {
	$entry = FrmEntry::getOne( $entry_id );
	  if ( in_array( $entry->form_id, array( 5, 6, 7 ) ) ) { // change 5, 6, and 7 to your form ids
	  	global $wpdb;
	  	$table = $wpdb->prefix . 'frm_items';
	  	$wpdb->update( $table, array( 'parent_item_id' => 0 ), array( 'parent_item_id' => $entry_id ) );
	  }
}
Formidable Hook - Helpful

Delete user

Submitted by  — 8 years ago

Use this code to delete a WordPress user from your site when their entry is deleted. Just replace 10 with your form ID and 25 with the ID of your userID field. Warning: This action cannot be undone.

add_action('frm_before_destroy_entry', 'delete_user_with_entry');
function delete_user_with_entry( $entry_id ) {
    $form_id = 10;// Replace 10 with the ID of your form
    $field_id = 25;//Replace 25 with the ID of your userID field
    $entry = FrmEntry::getOne( $entry_id, true );
    if ( $entry->form_id == $form_id ) {
        if ( isset( $entry->metas[ $field_id ] ) && $entry->metas[ $field_id ] ) {
            $user_id = $entry->metas[ $field_id ];
            wp_delete_user( $user_id );
        }
    }
}
If you would like to add your own example, click here.

Have something to add?

Click here to provide feedback on this page.