I would like admins to be able to delete users from the frontend with the click of a button. How could I use the wp_delete_users() function to create such a button on the frontend if the user ID is provided?
You could use AJAX to request a custom 'action' and send the users' ID. Alternatively 'post' the action and user ID to the same page. They are both essentially the same thing, but the former doesn't require the page to be reloaded.
There are plenty of topics on this site that deal with AJAX, so I'll omit the details (check out the ajax tag for more information).
Either way, you want to set action to 'myprefix_delete_user', and 'user_id' to the ID of the appropriate user.
Sending an ajax request with action 'myprefix_delete_user' will fire the hooks:
We only want to do something if are logged in, so we attach a callback to only the first hook:
(wasn't sure what to call this method...). Works in much the same way. You again 'post' the action and user_id variables. You can do this by constructing a link:
This 'posts' (not quite) the data to the current page. Then you hook onto '
The same callback function can be used, with the following changes:
Note, I have not performed any nonce or permission checks. You really should do this. The later is a simple
This question is broadly similar and may help:
The above answer has an error and an omission (not sure why it has 4 checks)
2) You need to
I wanted to delete users without going to admin dashboard - Here is working code solution:
Action happens when you post this
Add to your functions.php