Skip Navigation

[Resolved] Account Deletion does not send an email and neither does it show confirmation

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 3 months, 3 weeks ago.

Assisted by: Waqar.

Author
Posts
#2676562

I am looking to give user the ability to delete their account via a toolset form.

Code used for deletion:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_action('cred_save_data', 'deleteuser_v1', 10, 2);
function deleteuser_v1($post_id, $form_data)
{
    if ($form_data['id'] == form-id)
    {
     $userid = $post_id;
     //only delete if user is subscriber or member but not admin
       $user = wp_get_current_user();
   $user_roles = (array) $user->roles;
if ( in_array( 'subscriber', $user_roles ) || in_array( 'member', $user_roles ) && ( !in_array( 'employer', $user_roles ) || !in_array( 'administrator', $user_roles )) ) {

  wp_delete_user( $userid );

}  // if to ensure only subscriber or member is deleted
    } // right form
} // end of function

It works to delete the account but it does not send an email to Admin and it does not show the confirmation message (setup on the form) to the user from Toolset form.

Delete Account Page: hidden link

Delete User form: hidden link

Delete User Email setup: hidden link

#2676764

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

Have you tried replacing the 'cred_save_data' hook with the 'cred_success_redirect' hook?
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect )

The idea is to make the 'wp_delete_user' operation fire as late as possible, so that form has completed its other functions.

I'll recommend setting the form to redirect to a specific page and then test with the 'cred_success_redirect' hook.

regards,
Waqar

#2676977

I tried and this does not work . The user does not get deleted.

add_filter('cred_success_redirect', 'deleteuser_v1',10,3);
function deleteuser_v1($url, $post_id, $form_data)
{
    if ($form_data['id'] == form-id)
    {
     $userid = $post_id;
     //only delete if user is subscriber or member but not admin
       $user = wp_get_current_user();
   $user_roles = (array) $user->roles;
if ( in_array( 'subscriber', $user_roles ) || in_array( 'member', $user_roles ) && ( !in_array( 'employer', $user_roles ) || !in_array( 'administrator', $user_roles )) ) {

  wp_delete_user( $userid );

}  // if to ensure only subscriber or member is deleted
    } // right form
  return $url;
} // end of function

What am I missing?

#2677111

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

I'll recommend a different approach where the the actual user account deletion process is not directly linked to the form's processing.

For example, after successful submission of the form, you can redirect the visitor to a secret/special page. On that page you can load a custom shortcode that deletes the current user who visits the page, using the same 'wp_delete_user' function.

This way the form's own operations like email notification won't be affected by the user deletion.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.