Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wp_delete_user function to delete the user from front end is not working

Tags:

wordpress

I am working on a wordpress site. When a user login to the site, he is redirected to terms and conditions page. So if he accept terms and conditions, he will be allowed to use services and if he don't accept terms and conditions, his account will be terminated. Now I used the wp_delete_user function in order to terminate his account but its not working. Here is my code

add_action('init', 'function_init_terms');

function function_init_terms()
{
    if(isset($_POST['terms_submit']))
    {
        if($_POST['terms_agree'] == 'yes')
        {
            wp_redirect( get_permalink(215)); exit;
        }
        if($_POST['terms_agree'] != 'yes')
        {

            $user_ID = get_current_user_id();

             wp_delete_user($user_ID);
                echo html_entity_decode( wp_logout_url(home_url()) );




        }
    }
}

The first part of the code is working fine i.e if he is agreed with terms and conditions, he is redirected to page id 215 successfully. But the other part is not working. Here is my html

<form method="POST" action="<?php bloginfo('url') ?>/">
 <input type="checkbox" name="terms_agree" id="terms_agree" value="yes"/>Agree<br>
 <input type="submit" name="terms_submit" id="terms_submit" value="Continue" />
 </form>
like image 980
Riz Avatar asked Oct 15 '25 02:10

Riz


1 Answers

To make this post more readable for googlers, the answer from Adreivictor:

To use the back-end function wp_delete_user(), you need to require_once(ABSPATH.'wp-admin/includes/user.php' );

Example from Wordpress:

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 );
}

https://codex.wordpress.org/Function_Reference/wp_delete_user#Examples

like image 59
ejazz Avatar answered Oct 18 '25 08:10

ejazz