Skip Navigation

[Resolved] giving client the ability to delete users fro the front-end

This support ticket is created 6 years, 5 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 3 replies, has 2 voices.

Last updated by Beda 6 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#923217

Tell us what you are trying to do?
I am building a membership site for a client who wants to enter and edit users' details from the front end. The client doesn't want to deal with the back end of the site ...and I don't want them messing around there either.

I have been able to get the "enter a new user"; "show a list of users" and "edit a user" functions all working well. However, I'm really struggling to find a way to delete a user from the front end. (Along the way, I tried something that ended up deleting myself as a user. 🙁 I had backups, so I was able to recover.)

It seems that it's possible to give a user the ability to delete their own account, but I need to be able to have the site administrator able to delete a user from the front end.

Is this possible?

Is there any documentation that you are following?
I think I've read everything I can find on your site.

Is there a similar example that we can see?

What is the link to your site?
hidden link

Kindest regards,

Doug

#923396

WordPress doesn't really offer anything to delete a Profile yourself within a site.
It usually is done by an Admin by deleting the user itself.

Toolset Forms offer nothin to delete Users, although there has been some work on this feature in past, and eventually we will come back to it, but for now there is no ETA or confirmation about it.

What you can do, is use Forms where you edit an user, and bind an action to it with the Toolset Forms API.

Let's say you want a Delete Button where either logged in user with access to that page/list can delete the user which the button applies to.

You could create the classic List of Users which you use already to display Users Edit Forms or links.
Just, this time you use a new Edit User Form.

That Form has no fields, but the Submit Button which says "delete me".

Then, with PHP you create a code that deletes the user which you can grab from the Toolset Form, using the WordPress Codex:
wp_delete_user()
https://codex.wordpress.org/Function_Reference/wp_delete_user
Note that content might need to get reassigned.

This code, you can fire it when you click on "Delete me" (submit the actual Edit user form).
This is done by firing your custom code in the cred_save_data() hook explained here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

$post_id in this case will be the User ID.

A simple example that explains the logic and structure, but needs to be adapted and tested before you apply it:

add_action('cred_save_data', 'my_save_data_action',10,2);

function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12) //only happens on specific form
    {
       $reassign = '1';//This is the first user created, usually admin
       wp_delete_user( $post_id, $reassign );
    }
}

Make sure to redirect the form on submit to some other page, as the form cannot display anymore after you delete the User.

#924075

Thanks, Beda.

I'll read this thru a few times and then try it on a staging copy of the site.

I'm not quite following the logic just yet, as I say, I'll read it thru slowly a few more times.

I'd like to leave this ticket open so that I can come back to you in a day or two.

Thanks,

Doug

#924497

Ok, please let me know if anything is unclear, if I can explain better any particular step or you need more details on any of them.