We know you can create a user along with an entry. But when you delete the user entry the WordPress user remains, and vice versa. Here's some code that will eliminate a user's entries if you delete their WP user account, and if you delete the user account entry it will delete their WP account. NOTE: There is a frm_after_destroy_entry hook which would be preferable but it doesn't seem to work and FrmEntry::destroy() does a bunch of processing that sends it into an infinite loop. Also, if you try to delete a WP user with wp_delete_user() it times out, even if you include the WP script file. That's why why this snippet is written as such.
Specifying the $form1 (and optional $form2) will control which forms are appropriate.
// Delete user entries with user and vice versa
function my_frm_delete_user( $entry_id ) { $form1 = 1; $form2 = 2; global $wpdb; $frm_items = $wpdb->prefix . 'frm_items'; $form = $wpdb->get_var("SELECT form_id FROM $frm_items WHERE id = '$entry_id'"); if ( $form == $form1 || $form == $form2 ) { $user_id = $wpdb->get_var("SELECT user_id FROM $frm_items WHERE id = '$entry_id'"); if ( !empty($user_id) ) { $users = $wpdb->prefix . 'users'; $usermeta = $wpdb->prefix . 'usermeta'; $wpdb->delete($users, array('ID' => $user_id)); $umeta_ids = $wpdb->get_col("SELECT umeta_id FROM $usermeta WHERE user_id = '$user_id'"); foreach ($umeta_ids as $umeta_id) { $wpdb->delete($usermeta, array('umeta_id' => $umeta_id)); } $entradas = $wpdb->get_col("SELECT id FROM $frm_items WHERE user_id = '$user_id'"); foreach ($entradas as $entrada) { if ( $entrada == $entry_id ) continue; $wpdb->delete($frm_items, array('id' => $entrada)); $wpdb->delete($frm_item_metas, array('item_id' => $entrada)); } } } } add_action( 'frm_before_destroy_entry', 'my_frm_delete_user', 10, 1 ); function my_wp_delete_user( $user_id ) { global $wpdb; $frm_items = $wpdb->prefix . 'frm_items'; $entradas = $wpdb->get_col("SELECT id FROM $frm_items WHERE user_id = '$user_id'"); foreach ($entradas as $entrada) { $wpdb->delete($frm_items, array('id' => $entrada)); $wpdb->delete($frm_item_metas, array('item_id' => $entrada)); } } add_action( 'delete_user', 'my_wp_delete_user' );
Please login or Register to submit your answer