Skip Navigation

[Resolved] Administrators and Editors option to delete other user on the frontend

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

Problem: I would like to allow Administrators and Editors to delete Users from the front-end of the site.

Solution: You can use an Edit User Form and some custom code to handle this.

[cred_generic_field field='user-to-delete' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[wpv-user field="ID"],
"persist":1
}
[/cred_generic_field]
function tssupp_delete_user( $user_id, $form_data ){
  
    if ( 203 == $form_data['id'] ) {
  
        require_once( ABSPATH.'wp-admin/includes/user.php' );
  
        $current_user = $_POST['user-to-delete'];
  
        wp_delete_user( $current_user );
    }
}
add_action( 'cred_submit_complete', 'tssupp_delete_user', 10, 2 );

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://codex.wordpress.org/Function_Reference/wp_delete_user

This support ticket is created 6 years, 4 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by Nashaat 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#922054

Hi,
I have followed this instructions here: https://toolset.com/forums/topic/add-the-link-delete-account/
this work just fine for deleting own accounts.

now what i am trying to do is to insert an embed Cred Form button "delete user" in the loop of users view. so when listing all users a "delete user" button show up there for Administrator/Editors Role, so they can delete the account of other users from frontend.

is it possible to achieve this? if its not possible in the loop will this be possible with a different way? Main Idea that Admin/Editro can delete other users from frontend.

Thanks

#922197

You can include an Edit User Form in the Loop Output editor, which will insert one for each result in the View of Users. In the Form, include a generic field that holds the ID of the current User in the loop. Then in the cred_submit_complete hook, you can access that User's ID in the $_POST superglobal. So if your generic field name is "user-to-delete" then the cred_submit_complete code might look like this:

/**
 * Hook into CRED Edit User form submission
 * to delete current user
 */
function tssupp_delete_user( $user_id, $form_data ){
 
    if ( 203 == $form_data['id'] ) {
 
        require_once( ABSPATH.'wp-admin/includes/user.php' );
 
        $current_user = $_POST['user-to-delete'];
 
        wp_delete_user( $current_user );
    }
}
add_action( 'cred_submit_complete', 'tssupp_delete_user', 10, 2 );
#922824

Thanks Christian! I will try this and let you know if this works soon!

#923129

Okay sure, I will mark this ticket as pending an update from you. No need to reply right now. The ticket will remain open for 30 days.

#951419

Hi Christian,

could you please give me an example how to include in the edit user form a generic field that holds the ID of the current User in the loop

#951516

Something like this:

[cred_generic_field field='generic-user-field' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[wpv-user field="ID"],
"persist":1
}
[/cred_generic_field]
#952266
4-Views-Users-Frontend.png
3-Views-Users.png
2-User-Form-Content.png
1-User-Edit-Form.png

I have added the generic field to the form.

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

	[cred_field field='form_messages' value='' class='alert alert-warning']


[cred_generic_field field='user-to-delete' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[wpv-user field="ID"],
"persist":1
}
[/cred_generic_field]


	[cred_field field='form_submit' value='delete user' urlparam='' class='btn bg-danger text-white btn-sm' output='bootstrap']

[/creduserform]

then i have added this function.

/**
 * Hook into CRED Edit User form submission
 * to delete current user
 */
function tssupp_delete_user( $user_id, $form_data ){
  
    if ( 203 == $form_data['id'] ) {
  
        require_once( ABSPATH.'wp-admin/includes/user.php' );
  
        $current_user = $_POST['user-to-delete'];
  
        wp_delete_user( $current_user );
    }
}
add_action( 'cred_submit_complete', 'tssupp_delete_user', 10, 2 );

i have added the button in the loop but when i click on delete button the user wont be deleted.
i am the administrator with the name "admin123" and trying to delete a shop manager role user with the name "nasha"

#952285

My Bad! I forgot to change the Form ID number "203"

Now it works perfectly. Thank you Chris!