Delete User via Frontend

by Prusakov   Last Updated September 11, 2019 19:08 PM - source

I have made a table list of registered users. It is accessible by admin role only.

<table class="table table-striped w-100">
      <thead>
        <tr>
          <th>Clock Number</th>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Departament</th>
          <th>Email</th>
          <th>Link</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <?php
          $blogusers = get_users();
          foreach ($blogusers as $user) {
    echo '<tr>';
    echo '<td>' . esc_html($user->user_login) . '</td>';
    echo '<td>' . esc_html($user->first_name) . '</td>';
    echo '<td>' . esc_html($user->last_name) . '</td>';
    echo '<td>' . esc_html($user->roles[0]) . '</td>';
    echo '<td>' . esc_html($user->user_email) . '</td>'; ?>
    <td><a href="<?php echo site_url($path = '/author/' . $user->user_login); ?>">Edit</a></td>
    <?php
    echo '</tr>';
} ?>
      </tbody>
    </table>

There is a link to the author page (author.php) to display that user information. I can get all information by:

$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

I am trying to make a button to delete user however, this does not seem to work. If I create a function in function.php and assign user ID manually than its fine but I am trying to make it work when the delete button is hit. What is missing here?

    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
echo $curauth->ID;

if (isset($_POST["employee_user_login"]) && wp_verify_nonce($_POST['employee_delete_nonce'], 'employee-delete-nonce')) {
  require_once(ABSPATH.'wp-admin/includes/user.php');
  $current_user = $curauth->ID;
  $success = wp_delete_user($current_user);
  if ($success) {
    wp_redirect(home_url($path = '/site-settings', $scheme = null), $status = 302);
  }
}
?>
<div class="card w-50">
  <form id="employee_registration_form" class="site_form" autocomplete="off" action="" method="POST">
    <div class="row center">
      <p class="w-50">
        <input type="hidden" name="employee_delete_nonce" value="<?php echo wp_create_nonce('employee-delete-nonce'); ?>"/>
        <input type="submit" value="<?php _e('Delete Employee'); ?>"/>
      </p>
    </div>
  </form>
</div>


Related Questions


Creating front end admin area for users

Updated March 05, 2019 23:08 PM

Change author base and slug in author link

Updated February 20, 2019 09:08 AM



Save Hist in Author Pages by Date (Year and Month)

Updated March 21, 2016 08:03 AM