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
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 );
Thanks Christian! I will try this and let you know if this works soon!
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.
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
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"
My Bad! I forgot to change the Form ID number "203"
Now it works perfectly. Thank you Chris!