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
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
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?
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.