0

I'm using front page delete links for users to delete their own posts and comments.

The links are generated on the page like this :

 <a href="#" data-id="<?php the_ID() ?>" data-nonce="<?php echo wp_create_nonce('my_delete_post_nonce') ?>" class="stream-delete"></a>

 <a href="#" data-id="<?php comment_ID() ?>" data-nonce="<?php echo wp_create_nonce('my_delete_comment_nonce') ?>" class="comment-delete"></a>

Then I'm using the following to handle the request with AJAX :

 jQuery( document ).ready( function($) {
$(document).on( 'click', '.stream-delete', function() {
    var r = confirm("Delete This Post?");
    if (r == true) {
    var id = $(this).data('id');
    var nonce = $(this).data('nonce');
    var post = $(this).parents('.post:first');
    var space = (" ");
    $.ajax({
        type: 'post',
        url: MyAjax.ajaxurl,
        data: {
            action: 'my_delete_post',
            nonce: nonce,
            id: id
        },
        success: function( result ) {
            if( result == 'success' ) {
                $('#post-' + id).fadeOut('slow');
            }
        }
    })
    }
    return false;
})

$(document).on( 'click', '.comment-delete', function() {
    var r = confirm("Delete This Comment?");
    if (r == true) {
    var id = $(this).data('id');
    var nonce = $(this).data('nonce');
    var post = $(this).parents('.post:first');
    var space = (" ");
    $.ajax({
        type: 'post',
        url: MyAjax.ajaxurl,
        data: {
            action: 'my_delete_comment',
            nonce: nonce,
            id: id
        },
        success: function( result ) {
            if( result == 'success' ) {
                $('#div-comment-' + id).fadeOut('slow');
            }
        }
    })
    }
    return false;
  })
 })

And here are the functions :

 add_action( 'wp_ajax_my_delete_post', 'my_delete_post' );
    function my_delete_post(){
    $permission = check_ajax_referer( 'my_delete_post_nonce', 'nonce', false );
       if( $permission == false ) {
          echo 'error';
       }
       else {
             wp_delete_post( $_REQUEST['id'], true );
             echo 'success';
            }
     die();
    }

 add_action( 'wp_ajax_my_delete_comment', 'my_delete_comment' );
    function my_delete_comment(){
    $permission = check_ajax_referer( 'my_delete_comment_nonce', 'nonce', false );
      if( $permission == false ) {  
         echo 'error';
      }
      else {    
            wp_delete_comment( $REQUEST['id'], true );  
            echo 'success';
           }
     die();
    }

Delete Posts works perfectly, no issues.

Delete comments fires the success callback so the jQuery function does it's thing and hides the comment div, but the actual comment isn't being deleted from the database. Refresh the page and it's still there. It's like wp_delete_comment is doing nothing? It's throwing no errors in the log either.

Both of these actions are being performed on a custom post type if that's relevant.

  • Maybe changing $REQUEST['id'] to $_REQUEST['id'] fix the problem. – cybmeta Oct 20 '15 at 14:41
  • Ahhhhh! Code blind :) Thanks @cybmeta obviously that fixed it ;) – Grant Oct 20 '15 at 15:09

Your Answer

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

Browse other questions tagged or ask your own question.