Delete users from front end

By: Viktor D | Asked: 08/12/2023
ForumsCategory: Code SnippetsDelete users from front end
Viktor D asked 8 months ago
There\'s a code snippet to delete the users from front end: https://formidableforms.com/knowledgebase/frm_before_destroy_entry/#kb-delete-user This code is for a single form. How do I change this snippet to include multiple or all forms?  Thanks
2 Answers
Victor Font answered 8 months ago
Remove this if statement and closing bracket: if ( $entry->form_id == $form_id ) {
Viktor D replied 8 months ago

<p>I removed the if statement and the closing bracket but that still does not allow me to delete users from the front end. </p><pre>Start your code here//Delete user from Front End
add_action('frm_before_destroy_entry', 'delete_user_with_entry');
function delete_user_with_entry( $entry_id ) {
$form_id = 9;// Also need to add form_id = 6, 2, 3,
$field_id = 80;//Replace 25 with the ID of your userID field
$entry = FrmEntry::getOne( $entry_id, true );

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( 'um_adminchurch', (array) $user->roles ) ) {
wp_delete_user( $user->ID );
}
}

}</pre>

Victor Font replied 8 months ago

How are you trying to delete users from the front end by deleting a form entry? Please explain your process in more detail.

Viktor D replied 8 months ago

Victor,
I have user1 with user role to add/delete people. This User1 adds students using form9, also can add counselors with form6, assistants with form2, and subscribers with form3.

I'm using the edit and delete in-place shortcode as shown on this web page: https://formidableforms.com/knowledgebase/set-up-front-end-editing/#kb-edit-and-delete-in-place

I need the code to delete users from front end to include form2, 3, 6 and 9. Or maybe there's a way to change this code to include all the forms instead of listing them in the code snippet.
Thanks

Victor Font replied 8 months ago

The code you are trying to use deletes users from the wp_users table. If you are adding/deleting users through a form, are you using the User Registration add-on? You won't have any users in the wp_users table unless they are registered in WordPress. Are your users being added to the wp_users table?

Viktor D replied 8 months ago

Yes, I'm using the User Registration add-on.

Victor Font replied 8 months ago

If all of your users are being created by User1, how are you linking the new user ids created by the User Registration add-on to the form entry entered by User1? By default, the user id recorded in the entry will be that of the user entering the data.

Viktor D replied 8 months ago

I'm new to Formidable Forms plugin, so I'm not 100% sure how it works to answer your question.

The forms I listed above have permissions checked "allow logged-in users to create new users with this form" in the settings. So user1 can add users using these forms. Everything works good, including the delete-user code snippet I wrote about in the first message, but I need the code to be changed so that instead of just one form, to include forms 2, 3, 6, and 9.

Victor Font replied 8 months ago

You keep changing the requirement. You started asking about all forms, not you're listing 4. It makes a difference.

You can use in_array for the forms in the original if statement that needs to be there again because of the change in your requirement. Field ID will be different for each form, you'll have to retrieve the values separately based on the form id. You may want to use a switch statement.

Carlos Vázquez answered 8 months ago

Hello. To delete users, all you need is a view with a table of users with delete options (in form options), and that is associated with the registration form: <span class="anyclass">[deletelink label="Delete"]</span> Then it doesn't matter how many forms you have users. When you delete it in one, you will do it in all. This code deletes users in both directions: Wordpress>Formidable and Formidable>Wordpress 

// Delete from WordPress when deleted in Formidable
add_action('frm_before_destroy_entry', 'delete_user_with_entry');
function delete_user_with_entry($entry_id) {
     $form_id = 2; // Replace 2 with the ID of your form
     $field_id = 6; // Replace 6 with the ID of your userID field
     $entry = FrmEntry::getOne($entry_id, true);
     if ($entry->form_id == $form_id) {
         if (isset($entry->goals[$field_id]) && $entry->goals[$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, 1); // Change 1 to the user ID to whom the deleted user's post/pages will be assigned (this can be found in your wp_users table)
             }
         }
     }
}

// Remove from Formidable when removed from wordpress

add_action('delete_user', 'delete_formidable_entry_with_user');

function delete_formidable_entry_with_user($user_id) {
     static $processed_users = array();

     if (in_array($user_id, $processed_users)) {
         return;
     }

     $processed_users[] = $user_id;

     global $wpdb;

     $form_id = 2; // ID of your form
     $field_id = 6; // ID of the field containing the user ID

     $query = $wpdb->prepare(
         "SELECT item_id FROM {$wpdb->prefix}frm_item_metas WHERE field_id = %d AND meta_value = %s",
         $field_id,
         $user_id
     );

     $entry_id = $wpdb->get_var($query);

     if ($entry_id) {
         FrmEntry::destroy($entry_id);
     }
}

// END Delete users from WordPress when deleted from Formidable and vice versa

 You put it in your functions.php file and you're done. Remember to adjust the commented fields

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