Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create a user management via front end but i am not sure how to implement wp_delete_user Delete link

add_action( 'wp_head', 'my_action_javascript' );

function my_action_javascript() {
?>
<script type="text/javascript" >
jQuery(document).ready(function () {

    jQuery(".delete_user").click(function () {

        var data = {
            'action': 'delete_user_action',
                'user_id': jQuery(this).attr('delete-user-id'),

                'security': ajax_object.ajax_nonce
        };
        jQuery.post(ajax_object.ajax_url, data, function (response) {

        });

    });

});
</script>
<?php
}

add_action( 'wp_ajax_delete_user_action', 'delete_user_action_callback' );
add_action( 'wp_ajax_nopriv_delete_user_action', 'delete_user_action_callback' );

function delete_user_action_callback(){
  check_ajax_referer( 'security-special-string', 'security' );
  wp_delete_user( $_POST['user_id'] );
  die();
}


add_shortcode('listuser', 'listsubscriber');
function listsubscriber() {
$blogusers = get_users( array(  'fields' => array('display_name','ID' )) );

echo '<ul>';
foreach ( $blogusers as $user ) {

    echo '<li>' . esc_html( $user->display_name )  . '|' . esc_html( $user->ID )  . '<a class="delete_user" delete-user-id="' . $user->ID . '" href="#">Delete User</a></li>';

}
echo '</ul>';

}

    }
share|improve this question

1 Answer 1

up vote 0 down vote accepted

WordPress does not provide delete user link as it provides for deleting posts. Therefore, we should do this using Ajax. Following would be the approach towards solving this issue:

  1. Add user id as attribute to a tag
  2. When anchor is clicked, get the user id and fire ajax request which would delete the user.

Kindly, remove $deleteurl = wp_delete_user ( $user->ID); from the code. Because it will delete the user before listing and hence will throw error. We will add it in ajax call.

Lets start with adding attribute to anchor tag. We will add user id attribute as well as class to the anchor tag. So your link will look like:

echo '<li>' . esc_html( $user->display_name )  . '|' . esc_html( $user->ID )  . '<a class="delete_user" delete-user-id="' . $user->ID . '" href="#">Delete User</a></li>';

Add below jQuery code in your js file. You will need to pass admin_url( 'admin-ajax.php' ) as an ajax url to your js file. wp_localize_script can be used to pass php values to javascript. You can read more about Ajax in WordPress here.

I'll assume that Ajax url in js file would be accessible using ajax_object.ajax_url. Please change this parameter with your own. Also pass nonce field using wp_localiize_script. So in your array which would be passed using wp_localize_script will have one more key like this : 'ajax_nonce' => wp_create_nonce( "security-special-string" ). This is required for security purposes

You don't need document.ready statement if you have already called it and in that case start using code from jQuery(".delete_user").click(function(){ line.

jQuery(document).ready(function(){

  jQuery(".delete_user").click(function(){

        var data = {
        'action': 'delete_user_action',
        'user_id': jQuery(this).attr('delete-user-id'), //Pass User id
        'security': ajax_object.ajax_nonce
      };

      //lets fire ajax request
      jQuery.post(ajax_object.ajax_url, data, function(response) { 
        //Add your code here to show message after deleting the user.
      }
  });

});

Now lets write a code to handle the ajax request. You can write this code in your plugin's php file or in functions.php of your theme.

add_action( 'wp_ajax_delete_user_action', 'delete_user_action_callback' );
add_action( 'wp_ajax_nopriv_delete_user_action', 'delete_user_action_callback' );

function delete_user_action_callback(){
  check_ajax_referer( 'security-special-string', 'security' );
  wp_delete_user( $_POST['user_id'] );
  die();
}

For security purpose, you should check if user has permissions to delete user using if( current_user_can( 'delete_users' ) ) condition.

Update

Thank you for posting your code. We will need to make modifications in that code.

Please try this code:

add_action( 'wp_head', 'my_action_javascript' );

function my_action_javascript() {

        ?>
        <script type="text/javascript" >
                jQuery(document).ready(function() {

                        jQuery(".delete_user").click(function() {

                                var current_element_var = jQuery(this);

                                var data = {
                                        'action': 'delete_user_action',
                                        'user_id': current_element_var.attr('delete-user-id'),
                                        'security': '<?php echo wp_create_nonce( "security-special-string" ) ?>'
                                };
                                jQuery.post('<?php echo admin_url( 'admin-ajax.php' ) ?>', data, function(response) {
                                        if (response == 'deleted_successfully') {
                                                current_element_var.hide();
                                                current_element_var.after('<span> User Deleted Successfully </span>');
                                                current_element_var.remove();
                                        }
                                });

                                return false;

                        });

                });
        </script>
        <?php

}

add_action( 'wp_ajax_delete_user_action', 'delete_user_action_callback' );
add_action( 'wp_ajax_nopriv_delete_user_action', 'delete_user_action_callback' );

function delete_user_action_callback() {
        check_ajax_referer( 'security-special-string', 'security' );
        wp_delete_user( $_POST['user_id'] );
        echo 'deleted_successfully';
        die();

}

Let me know if above code works for you. :)

share|improve this answer
    
thanks I have updated the code with the reference you gave, I also made adjustment on the syntax on jquery since it is returning invalid. All the code is added in the functions.php of the file but the problem is when I click the delete link button nothing happens –  JPL Jul 31 at 16:42
    
Hi, I have updated the code as per changes. Please check if section after 'Update' text works. –  WisdmLabs Jul 31 at 17:53
    
thanks for your help it is working now ill try to put edit links in the future which will allow the admin to edit any user in the front end –  JPL Aug 1 at 13:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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