This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
This hook allows you to do something before an entry is deleted.
Usage
add_action('frm_before_destroy_entry', 'my_custom_function'); function my_custom_function($entry_id)
Parameters
- $entry_id (integer)
- $entry (object) The data of the entry to be deleted
Examples
Basic Example
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
}
}
Delete Uploads
As of v2.0.22, this is no 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.
<script src="https://gist.github.com/fb60fc55adb212cb0dd8.js?file=frm_delete_files.php" type="text/javascript"></script>
Separate child entries
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 ) );
}
}
Delete user
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 ];
$user = new WP_User( $user_id );
if ( ! in_array( 'administrator', (array) $user->roles ) ) {
wp_delete_user( $user_id, 7 );//change 7 to the user ID to whom the deleted user's post/pages will be assigned (this can be found in your wp_users table)
}
}
}
}
If you would like to remove the user and their posts/pages, then use this line at the end instead:
wp_delete_user( $user->ID );
Access a row on a repeater
Repeating groups are child form entries, and when they are deleted, this hook is called for each repeating group row.
add_action( 'frm_before_destroy_entry', 'my_custom_function',9 );
function my_custom_function( $entry_id ) {
$entry = FrmEntry::getOne( $entry_id, true );
if ( $entry->parent_item_id !== '0' ) {
// do something for repeating group entries from this form
}
}