Skip Navigation

[Resolved] Add a front-end link for user’s to delete their account

This thread is resolved. Here is a description of the problem and solution.

Problem:
Is it possible to have a link on the front-end users can use to delete their account?

Solution:
1. Make sure the settings when editing the custom post type at Toolset > Post Types under "Sections to display when editing..." include the author option.
2. Create a CRED form to edit users, which need include only a "Delete User" button, like so:

[creduserform class='cred-user-form cred-keep-original']
 
    [cred_field field='form_submit' value='Delete User' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']
 
[/creduserform]

3. Add the following code to your theme's functions.php file or using a plugin such as Code Snippets, being sure to edit the id of the form created in the previous step:

/**
 * Hook into CRED Edit User form submission
 * to delete current user
 */
function tssupp_delete_user( $post_id, $form_data ){
 
    if ( 203 == $form_data['id'] ) {
 
        require_once( ABSPATH.'wp-admin/includes/user.php' );
 
        $current_user = get_current_user_id();
 
        wp_delete_user( $current_user );
    }
}
add_action( 'cred_submit_complete', 'tssupp_delete_user', 10, 2 );
This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 11 replies, has 3 voices.

Last updated by diyanK 6 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#605690

I would like to allow registered users to cancel their account, is it possible or should I install a plugin?

thank you
Massimo

#605761

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Massimo

There is no such feature to delete users from the front-end.

Deleting users from the backend requires deciding what to do with the content authored by the user, not just deleting the user itself and the user meta.

You can programmatically delete a user with wp_delete_user, passing the user id (which would be the current user id, retrievable with get_current_user_id) and a reassign parameter if you want to do anything other than delete the user's content.

https://developer.wordpress.org/reference/functions/wp_delete_user/
https://developer.wordpress.org/reference/functions/get_current_user_id/

What may be the trickier part is handling the user pressing a button on the front-end and triggering your code on the server to delete the user.

You could piggy-back on a CRED edit form.

So, make a CRED edit user form specifically for the purpose of deleting the user. It doesn't want to have any content other than the "Delete user" button and possibly some text warning about it being final.

Then use the CRED API, e.g. the cred_submit_complete hook (https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete) to trigger your code which takes the ID of the current user and deletes it.

Insert the cred edit form directly on a page where you want the delete user button to appear.

#605839

Hi Nigel and thank you for the very professional answer.

I bought Toolset because in advertising it said that "without a line of code" and "without being a programmer" you can make beautiful websites. (mine does not want to be a criticism)

Unfortunately I am not a programmer, but I am very happy to learn!
I do not understand how to use WordPress Hooks, do you have an easy-to-read guide?

I've seen that there are plugins that delete your account, but I do not want to fill my plugin site

thank you
Maximum

#605851

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Massimo

I just finished, so I'll help you with the code on Monday if that's okay.

#605852

ok - have a nice weekend !

#606430

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Massimo

I tried this out and it seems to work okay, so...

I made a CRED User Edit form. It's only content is the submit button with the text "Delete user".

So the markup for the entire form looks like this:

[creduserform class='cred-user-form cred-keep-original']

	[cred_field field='form_submit' value='Delete User' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/creduserform]

What should happen when users delete their account? In my test site I set the CRED form to redirect to the homepage on submission.

I then inserted this CRED form into its own page with some "Are you sure? This cannot be undone" text.

So when a user visits that page they will see just that text and the delete account button.

When using the delete account button to submit the form, I then add some custom code to hook into the process and delete the user.

The official documentation about how WordPress hooks work is here: https://developer.wordpress.org/plugins/hooks/

I'm using a custom hook provided by Toolset, cred_submit_complete (https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete)

Add the following code to your theme's functions.php or using a plugin such as Code Snippets:

/**
 * Hook into CRED Edit User form submission
 * to delete current user
 */
function tssupp_delete_user( $post_id, $form_data ){

	if ( 203 == $form_data['id'] ) {

		require_once( ABSPATH.'wp-admin/includes/user.php' );

		$current_user = get_current_user_id();

		wp_delete_user( $current_user );
	}
}
add_action( 'cred_submit_complete', 'tssupp_delete_user', 10, 2 );

Note that you will need to edit the ID of the CRED Edit User form with the delete button (203 in my sample code).

You probably ought to make sure you have a current backup of your site before you add this and start testing.

#606709
esempio30.jpg

Thanks Nigel for the guide,
I wanted to ask if I can insert a "delete" link instead of the button in the form.
I created the admin section (see attachment)

#606737

Hi Nigel,
it works, I managed to delete the user.
there is a problem:
- deleted user posts are NOT deleted

thank you

#606769

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Massimo

It didn't occur to me but wp_delete_post won't necessarily delete posts of all types.

I took a look at the source code and there is a delete_with_user setting available when registering a custom post type, but that is not available in the options when registering a custom post type with Types.

According to the documentation, if that is not set, it will fall back to checking the author setting for the post type.

So, for the post types created by this author, if you go to Toolset > Post Types and edit the post types, scroll to the section "Sections to display when editing..." and make sure the author box is checked.

You should then find that when deleting a user their posts are trashed.

#606808

Perfect !!!! It works !!!!

Thank you !!!

#1223294

Hi guys, can you please confirm this is still working? I'm having trouble implementing it. Though looks simple enough...

#1223439

Got it working but need wider functionality, will open a new ticket.