Delete user on delete entry, delete entries on delete user

By: Dr. Flow | Asked: 11/25/2022
ForumsCategory: Code SnippetsDelete user on delete entry, delete entries on delete user
Dr. Flow asked 2 years ago

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' );
3 Answers
Michael Parisi answered 11 months ago
This code appears to be incorrect.  Specifically, $frm_item_metas is missing a definition in both situations.
Michael Parisi answered 11 months ago
I'd also suggest you use tokens in your SQL, as this could end up with an injection if its passed a param from the user.
Michael Parisi answered 11 months ago
Yea, I tried to use this.  It's easier to just rewrite it as the code is broken and insecure.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right