2

Hi I am working on a frontend admin where a user must be able to delete his account. current_user() is a custom made function of me that retrieves the user meta data.

Does anyone know how I can make this work?

echo '<a class="remove" href="' . get_permalink() . '?remove_account">' . __( 'Click here to remove your account' ) . '</a>';

// Remove account
function terminate_account() {
    require_once( ABSPATH . 'wp-admin/includes/user.php' );
    wp_delete_user( current_user( 'ID' ) );
}

if( isset( $_GET['remove_account'] ) ) {
    add_action( 'init', 'terminate_account' );
}

1 Answer 1

1

Taken directly form the wp_delete_user documentation:

if(is_user_logged_in() && !empty($_GET['DeleteMyAccount'])) {
    add_action('init', 'remove_logged_in_user');
}

function remove_logged_in_user() {
    require_once(ABSPATH.'wp-admin/includes/user.php' );
    $current_user = wp_get_current_user();
    wp_delete_user( $current_user->ID );
}

Things to note:

  • Your code didn't check if the user was logged in or not
  • by the time you're printing the remove user link, the init action has already happened and finished, so your delete user wouldn't work. That part of the code needs to be ran earlier, say in functions.php or a plugin

edit:

If you can place this in your themes function file:

add_action('init', 'remove_logged_in_user');

function remove_logged_in_user() {
    require_once(ABSPATH.'wp-admin/includes/user.php' );
    $current_user = wp_get_current_user();
    $success = wp_delete_user( $current_user->ID );
    wp_die('wp delete gave: <pre>"'.print_r($success).'"</pre>');
}
12
  • I have tried it in functions.php but got the same result..
    – Robbert
    Mar 12, 2014 at 14:37
  • and you've verified that wp_delete_user is being called? what happens if you place wp_die('testing'); at the top of the remove_logged_in_user function?
    – Tom J Nowell
    Mar 12, 2014 at 17:35
  • Tried that, nothing happens..
    – Robbert
    Mar 13, 2014 at 8:57
  • And thats with the URL /?remove_account=true ? Also what's the return value of wp_delete_user ? That wp_die doesn't get executed suggests that the function never runs, hence the user never getting deleted
    – Tom J Nowell
    Mar 13, 2014 at 10:42
  • Yes that is with /?remove_account=true. No return value in my opinion..
    – Robbert
    Mar 13, 2014 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.