wp_ajax_delete_comment

The timeline below displays how wordpress function wp_ajax_delete_comment has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Handles deleting a comment via AJAX.
 *
 * @since 3.1.0
 */
function wp_ajax_delete_comment()
{
    $id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
    $comment = get_comment($id);
    if (!$comment) {
        wp_die(time());
    }
    if (!current_user_can('edit_comment', $comment->comment_ID)) {
        wp_die(-1);
    }
    check_ajax_referer("delete-comment_{$id}");
    $status = wp_get_comment_status($comment);
    $delta = -1;
    if (isset($_POST['trash']) && 1 == $_POST['trash']) {
        if ('trash' === $status) {
            wp_die(time());
        }
        $r = wp_trash_comment($comment);
    } elseif (isset($_POST['untrash']) && 1 == $_POST['untrash']) {
        if ('trash' !== $status) {
            wp_die(time());
        }
        $r = wp_untrash_comment($comment);
        // Undo trash, not in Trash.
        if (!isset($_POST['comment_status']) || 'trash' !== $_POST['comment_status']) {
            $delta = 1;
        }
    } elseif (isset($_POST['spam']) && 1 == $_POST['spam']) {
        if ('spam' === $status) {
            wp_die(time());
        }
        $r = wp_spam_comment($comment);
    } elseif (isset($_POST['unspam']) && 1 == $_POST['unspam']) {
        if ('spam' !== $status) {
            wp_die(time());
        }
        $r = wp_unspam_comment($comment);
        // Undo spam, not in spam.
        if (!isset($_POST['comment_status']) || 'spam' !== $_POST['comment_status']) {
            $delta = 1;
        }
    } elseif (isset($_POST['delete']) && 1 == $_POST['delete']) {
        $r = wp_delete_comment($comment);
    } else {
        wp_die(-1);
    }
    if ($r) {
        // Decide if we need to send back '1' or a more complicated response including page links and comment counts.
        _wp_ajax_delete_comment_response($comment->comment_ID, $delta);
    }
    wp_die(0);
}

WordPress Version: 5.5

/**
 * Ajax handler for deleting a comment.
 *
 * @since 3.1.0
 */
function wp_ajax_delete_comment()
{
    $id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
    $comment = get_comment($id);
    if (!$comment) {
        wp_die(time());
    }
    if (!current_user_can('edit_comment', $comment->comment_ID)) {
        wp_die(-1);
    }
    check_ajax_referer("delete-comment_{$id}");
    $status = wp_get_comment_status($comment);
    $delta = -1;
    if (isset($_POST['trash']) && 1 == $_POST['trash']) {
        if ('trash' === $status) {
            wp_die(time());
        }
        $r = wp_trash_comment($comment);
    } elseif (isset($_POST['untrash']) && 1 == $_POST['untrash']) {
        if ('trash' !== $status) {
            wp_die(time());
        }
        $r = wp_untrash_comment($comment);
        // Undo trash, not in Trash.
        if (!isset($_POST['comment_status']) || 'trash' !== $_POST['comment_status']) {
            $delta = 1;
        }
    } elseif (isset($_POST['spam']) && 1 == $_POST['spam']) {
        if ('spam' === $status) {
            wp_die(time());
        }
        $r = wp_spam_comment($comment);
    } elseif (isset($_POST['unspam']) && 1 == $_POST['unspam']) {
        if ('spam' !== $status) {
            wp_die(time());
        }
        $r = wp_unspam_comment($comment);
        // Undo spam, not in spam.
        if (!isset($_POST['comment_status']) || 'spam' !== $_POST['comment_status']) {
            $delta = 1;
        }
    } elseif (isset($_POST['delete']) && 1 == $_POST['delete']) {
        $r = wp_delete_comment($comment);
    } else {
        wp_die(-1);
    }
    if ($r) {
        // Decide if we need to send back '1' or a more complicated response including page links and comment counts.
        _wp_ajax_delete_comment_response($comment->comment_ID, $delta);
    }
    wp_die(0);
}

WordPress Version: 5.4

/**
 * Ajax handler for deleting a comment.
 *
 * @since 3.1.0
 */
function wp_ajax_delete_comment()
{
    $id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
    $comment = get_comment($id);
    if (!$comment) {
        wp_die(time());
    }
    if (!current_user_can('edit_comment', $comment->comment_ID)) {
        wp_die(-1);
    }
    check_ajax_referer("delete-comment_{$id}");
    $status = wp_get_comment_status($comment);
    $delta = -1;
    if (isset($_POST['trash']) && 1 == $_POST['trash']) {
        if ('trash' == $status) {
            wp_die(time());
        }
        $r = wp_trash_comment($comment);
    } elseif (isset($_POST['untrash']) && 1 == $_POST['untrash']) {
        if ('trash' != $status) {
            wp_die(time());
        }
        $r = wp_untrash_comment($comment);
        // Undo trash, not in Trash.
        if (!isset($_POST['comment_status']) || 'trash' !== $_POST['comment_status']) {
            $delta = 1;
        }
    } elseif (isset($_POST['spam']) && 1 == $_POST['spam']) {
        if ('spam' == $status) {
            wp_die(time());
        }
        $r = wp_spam_comment($comment);
    } elseif (isset($_POST['unspam']) && 1 == $_POST['unspam']) {
        if ('spam' != $status) {
            wp_die(time());
        }
        $r = wp_unspam_comment($comment);
        // Undo spam, not in spam.
        if (!isset($_POST['comment_status']) || 'spam' !== $_POST['comment_status']) {
            $delta = 1;
        }
    } elseif (isset($_POST['delete']) && 1 == $_POST['delete']) {
        $r = wp_delete_comment($comment);
    } else {
        wp_die(-1);
    }
    if ($r) {
        // Decide if we need to send back '1' or a more complicated response including page links and comment counts.
        _wp_ajax_delete_comment_response($comment->comment_ID, $delta);
    }
    wp_die(0);
}

WordPress Version: 5.3

/**
 * Ajax handler for deleting a comment.
 *
 * @since 3.1.0
 */
function wp_ajax_delete_comment()
{
    $id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
    $comment = get_comment($id);
    if (!$comment) {
        wp_die(time());
    }
    if (!current_user_can('edit_comment', $comment->comment_ID)) {
        wp_die(-1);
    }
    check_ajax_referer("delete-comment_{$id}");
    $status = wp_get_comment_status($comment);
    $delta = -1;
    if (isset($_POST['trash']) && 1 == $_POST['trash']) {
        if ('trash' == $status) {
            wp_die(time());
        }
        $r = wp_trash_comment($comment);
    } elseif (isset($_POST['untrash']) && 1 == $_POST['untrash']) {
        if ('trash' != $status) {
            wp_die(time());
        }
        $r = wp_untrash_comment($comment);
        if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'trash') {
            // undo trash, not in trash
            $delta = 1;
        }
    } elseif (isset($_POST['spam']) && 1 == $_POST['spam']) {
        if ('spam' == $status) {
            wp_die(time());
        }
        $r = wp_spam_comment($comment);
    } elseif (isset($_POST['unspam']) && 1 == $_POST['unspam']) {
        if ('spam' != $status) {
            wp_die(time());
        }
        $r = wp_unspam_comment($comment);
        if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'spam') {
            // undo spam, not in spam
            $delta = 1;
        }
    } elseif (isset($_POST['delete']) && 1 == $_POST['delete']) {
        $r = wp_delete_comment($comment);
    } else {
        wp_die(-1);
    }
    if ($r) {
        // Decide if we need to send back '1' or a more complicated response including page links and comment counts
        _wp_ajax_delete_comment_response($comment->comment_ID, $delta);
    }
    wp_die(0);
}

WordPress Version: 4.4

/**
 * Ajax handler for deleting a comment.
 *
 * @since 3.1.0
 */
function wp_ajax_delete_comment()
{
    $id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
    if (!$comment = get_comment($id)) {
        wp_die(time());
    }
    if (!current_user_can('edit_comment', $comment->comment_ID)) {
        wp_die(-1);
    }
    check_ajax_referer("delete-comment_{$id}");
    $status = wp_get_comment_status($comment);
    $delta = -1;
    if (isset($_POST['trash']) && 1 == $_POST['trash']) {
        if ('trash' == $status) {
            wp_die(time());
        }
        $r = wp_trash_comment($comment);
    } elseif (isset($_POST['untrash']) && 1 == $_POST['untrash']) {
        if ('trash' != $status) {
            wp_die(time());
        }
        $r = wp_untrash_comment($comment);
        if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'trash') {
            // undo trash, not in trash
            $delta = 1;
        }
    } elseif (isset($_POST['spam']) && 1 == $_POST['spam']) {
        if ('spam' == $status) {
            wp_die(time());
        }
        $r = wp_spam_comment($comment);
    } elseif (isset($_POST['unspam']) && 1 == $_POST['unspam']) {
        if ('spam' != $status) {
            wp_die(time());
        }
        $r = wp_unspam_comment($comment);
        if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'spam') {
            // undo spam, not in spam
            $delta = 1;
        }
    } elseif (isset($_POST['delete']) && 1 == $_POST['delete']) {
        $r = wp_delete_comment($comment);
    } else {
        wp_die(-1);
    }
    if ($r) {
        // Decide if we need to send back '1' or a more complicated response including page links and comment counts
        _wp_ajax_delete_comment_response($comment->comment_ID, $delta);
    }
    wp_die(0);
}

WordPress Version: 4.0

/**
 * Ajax handler for deleting a comment.
 *
 * @since 3.1.0
 */
function wp_ajax_delete_comment()
{
    $id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
    if (!$comment = get_comment($id)) {
        wp_die(time());
    }
    if (!current_user_can('edit_comment', $comment->comment_ID)) {
        wp_die(-1);
    }
    check_ajax_referer("delete-comment_{$id}");
    $status = wp_get_comment_status($comment->comment_ID);
    $delta = -1;
    if (isset($_POST['trash']) && 1 == $_POST['trash']) {
        if ('trash' == $status) {
            wp_die(time());
        }
        $r = wp_trash_comment($comment->comment_ID);
    } elseif (isset($_POST['untrash']) && 1 == $_POST['untrash']) {
        if ('trash' != $status) {
            wp_die(time());
        }
        $r = wp_untrash_comment($comment->comment_ID);
        if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'trash') {
            // undo trash, not in trash
            $delta = 1;
        }
    } elseif (isset($_POST['spam']) && 1 == $_POST['spam']) {
        if ('spam' == $status) {
            wp_die(time());
        }
        $r = wp_spam_comment($comment->comment_ID);
    } elseif (isset($_POST['unspam']) && 1 == $_POST['unspam']) {
        if ('spam' != $status) {
            wp_die(time());
        }
        $r = wp_unspam_comment($comment->comment_ID);
        if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'spam') {
            // undo spam, not in spam
            $delta = 1;
        }
    } elseif (isset($_POST['delete']) && 1 == $_POST['delete']) {
        $r = wp_delete_comment($comment->comment_ID);
    } else {
        wp_die(-1);
    }
    if ($r) {
        // Decide if we need to send back '1' or a more complicated response including page links and comment counts
        _wp_ajax_delete_comment_response($comment->comment_ID, $delta);
    }
    wp_die(0);
}

WordPress Version: 3.7

function wp_ajax_delete_comment()
{
    $id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
    if (!$comment = get_comment($id)) {
        wp_die(time());
    }
    if (!current_user_can('edit_comment', $comment->comment_ID)) {
        wp_die(-1);
    }
    check_ajax_referer("delete-comment_{$id}");
    $status = wp_get_comment_status($comment->comment_ID);
    $delta = -1;
    if (isset($_POST['trash']) && 1 == $_POST['trash']) {
        if ('trash' == $status) {
            wp_die(time());
        }
        $r = wp_trash_comment($comment->comment_ID);
    } elseif (isset($_POST['untrash']) && 1 == $_POST['untrash']) {
        if ('trash' != $status) {
            wp_die(time());
        }
        $r = wp_untrash_comment($comment->comment_ID);
        if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'trash') {
            // undo trash, not in trash
            $delta = 1;
        }
    } elseif (isset($_POST['spam']) && 1 == $_POST['spam']) {
        if ('spam' == $status) {
            wp_die(time());
        }
        $r = wp_spam_comment($comment->comment_ID);
    } elseif (isset($_POST['unspam']) && 1 == $_POST['unspam']) {
        if ('spam' != $status) {
            wp_die(time());
        }
        $r = wp_unspam_comment($comment->comment_ID);
        if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'spam') {
            // undo spam, not in spam
            $delta = 1;
        }
    } elseif (isset($_POST['delete']) && 1 == $_POST['delete']) {
        $r = wp_delete_comment($comment->comment_ID);
    } else {
        wp_die(-1);
    }
    if ($r) {
        // Decide if we need to send back '1' or a more complicated response including page links and comment counts
        _wp_ajax_delete_comment_response($comment->comment_ID, $delta);
    }
    wp_die(0);
}