wp_ajax_get_comments

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

WordPress Version: 6.3

/**
 * Handles getting comments via AJAX.
 *
 * @since 3.1.0
 *
 * @global int $post_id
 *
 * @param string $action Action to perform.
 */
function wp_ajax_get_comments($action)
{
    global $post_id;
    if (empty($action)) {
        $action = 'get-comments';
    }
    check_ajax_referer($action);
    if (empty($post_id) && !empty($_REQUEST['p'])) {
        $id = absint($_REQUEST['p']);
        if (!empty($id)) {
            $post_id = $id;
        }
    }
    if (empty($post_id)) {
        wp_die(-1);
    }
    $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array('screen' => 'edit-comments'));
    if (!current_user_can('edit_post', $post_id)) {
        wp_die(-1);
    }
    $wp_list_table->prepare_items();
    if (!$wp_list_table->has_items()) {
        wp_die(1);
    }
    $x = new WP_Ajax_Response();
    ob_start();
    foreach ($wp_list_table->items as $comment) {
        if (!current_user_can('edit_comment', $comment->comment_ID) && 0 === $comment->comment_approved) {
            continue;
        }
        get_comment($comment);
        $wp_list_table->single_row($comment);
    }
    $comment_list_item = ob_get_clean();
    $x->add(array('what' => 'comments', 'data' => $comment_list_item));
    $x->send();
}

WordPress Version: 5.5

/**
 * Ajax handler for getting comments.
 *
 * @since 3.1.0
 *
 * @global int $post_id
 *
 * @param string $action Action to perform.
 */
function wp_ajax_get_comments($action)
{
    global $post_id;
    if (empty($action)) {
        $action = 'get-comments';
    }
    check_ajax_referer($action);
    if (empty($post_id) && !empty($_REQUEST['p'])) {
        $id = absint($_REQUEST['p']);
        if (!empty($id)) {
            $post_id = $id;
        }
    }
    if (empty($post_id)) {
        wp_die(-1);
    }
    $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array('screen' => 'edit-comments'));
    if (!current_user_can('edit_post', $post_id)) {
        wp_die(-1);
    }
    $wp_list_table->prepare_items();
    if (!$wp_list_table->has_items()) {
        wp_die(1);
    }
    $x = new WP_Ajax_Response();
    ob_start();
    foreach ($wp_list_table->items as $comment) {
        if (!current_user_can('edit_comment', $comment->comment_ID) && 0 === $comment->comment_approved) {
            continue;
        }
        get_comment($comment);
        $wp_list_table->single_row($comment);
    }
    $comment_list_item = ob_get_clean();
    $x->add(array('what' => 'comments', 'data' => $comment_list_item));
    $x->send();
}

WordPress Version: 4.7

/**
 * Ajax handler for getting comments.
 *
 * @since 3.1.0
 *
 * @global int           $post_id
 *
 * @param string $action Action to perform.
 */
function wp_ajax_get_comments($action)
{
    global $post_id;
    if (empty($action)) {
        $action = 'get-comments';
    }
    check_ajax_referer($action);
    if (empty($post_id) && !empty($_REQUEST['p'])) {
        $id = absint($_REQUEST['p']);
        if (!empty($id)) {
            $post_id = $id;
        }
    }
    if (empty($post_id)) {
        wp_die(-1);
    }
    $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array('screen' => 'edit-comments'));
    if (!current_user_can('edit_post', $post_id)) {
        wp_die(-1);
    }
    $wp_list_table->prepare_items();
    if (!$wp_list_table->has_items()) {
        wp_die(1);
    }
    $x = new WP_Ajax_Response();
    ob_start();
    foreach ($wp_list_table->items as $comment) {
        if (!current_user_can('edit_comment', $comment->comment_ID) && 0 === $comment->comment_approved) {
            continue;
        }
        get_comment($comment);
        $wp_list_table->single_row($comment);
    }
    $comment_list_item = ob_get_clean();
    $x->add(array('what' => 'comments', 'data' => $comment_list_item));
    $x->send();
}

WordPress Version: 4.6

/**
 * Ajax handler for getting comments.
 *
 * @since 3.1.0
 *
 * @global WP_List_Table $wp_list_table
 * @global int           $post_id
 *
 * @param string $action Action to perform.
 */
function wp_ajax_get_comments($action)
{
    global $wp_list_table, $post_id;
    if (empty($action)) {
        $action = 'get-comments';
    }
    check_ajax_referer($action);
    if (empty($post_id) && !empty($_REQUEST['p'])) {
        $id = absint($_REQUEST['p']);
        if (!empty($id)) {
            $post_id = $id;
        }
    }
    if (empty($post_id)) {
        wp_die(-1);
    }
    $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array('screen' => 'edit-comments'));
    if (!current_user_can('edit_post', $post_id)) {
        wp_die(-1);
    }
    $wp_list_table->prepare_items();
    if (!$wp_list_table->has_items()) {
        wp_die(1);
    }
    $x = new WP_Ajax_Response();
    ob_start();
    foreach ($wp_list_table->items as $comment) {
        if (!current_user_can('edit_comment', $comment->comment_ID) && 0 === $comment->comment_approved) {
            continue;
        }
        get_comment($comment);
        $wp_list_table->single_row($comment);
    }
    $comment_list_item = ob_get_clean();
    $x->add(array('what' => 'comments', 'data' => $comment_list_item));
    $x->send();
}

WordPress Version: 4.3

/**
 * Ajax handler for getting comments.
 *
 * @since 3.1.0
 *
 * @global WP_List_Table $wp_list_table
 * @global int           $post_id
 *
 * @param string $action Action to perform.
 */
function wp_ajax_get_comments($action)
{
    global $wp_list_table, $post_id;
    if (empty($action)) {
        $action = 'get-comments';
    }
    check_ajax_referer($action);
    if (empty($post_id) && !empty($_REQUEST['p'])) {
        $id = absint($_REQUEST['p']);
        if (!empty($id)) {
            $post_id = $id;
        }
    }
    if (empty($post_id)) {
        wp_die(-1);
    }
    $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array('screen' => 'edit-comments'));
    if (!current_user_can('edit_post', $post_id)) {
        wp_die(-1);
    }
    $wp_list_table->prepare_items();
    if (!$wp_list_table->has_items()) {
        wp_die(1);
    }
    $x = new WP_Ajax_Response();
    ob_start();
    foreach ($wp_list_table->items as $comment) {
        if (!current_user_can('edit_comment', $comment->comment_ID)) {
            continue;
        }
        get_comment($comment);
        $wp_list_table->single_row($comment);
    }
    $comment_list_item = ob_get_clean();
    $x->add(array('what' => 'comments', 'data' => $comment_list_item));
    $x->send();
}

WordPress Version: 4.0

/**
 * Ajax handler for getting comments.
 *
 * @since 3.1.0
 *
 * @param string $action Action to perform.
 */
function wp_ajax_get_comments($action)
{
    global $wp_list_table, $post_id;
    if (empty($action)) {
        $action = 'get-comments';
    }
    check_ajax_referer($action);
    if (empty($post_id) && !empty($_REQUEST['p'])) {
        $id = absint($_REQUEST['p']);
        if (!empty($id)) {
            $post_id = $id;
        }
    }
    if (empty($post_id)) {
        wp_die(-1);
    }
    $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array('screen' => 'edit-comments'));
    if (!current_user_can('edit_post', $post_id)) {
        wp_die(-1);
    }
    $wp_list_table->prepare_items();
    if (!$wp_list_table->has_items()) {
        wp_die(1);
    }
    $x = new WP_Ajax_Response();
    ob_start();
    foreach ($wp_list_table->items as $comment) {
        if (!current_user_can('edit_comment', $comment->comment_ID)) {
            continue;
        }
        get_comment($comment);
        $wp_list_table->single_row($comment);
    }
    $comment_list_item = ob_get_contents();
    ob_end_clean();
    $x->add(array('what' => 'comments', 'data' => $comment_list_item));
    $x->send();
}

WordPress Version: 3.7

function wp_ajax_get_comments($action)
{
    global $wp_list_table, $post_id;
    if (empty($action)) {
        $action = 'get-comments';
    }
    check_ajax_referer($action);
    if (empty($post_id) && !empty($_REQUEST['p'])) {
        $id = absint($_REQUEST['p']);
        if (!empty($id)) {
            $post_id = $id;
        }
    }
    if (empty($post_id)) {
        wp_die(-1);
    }
    $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array('screen' => 'edit-comments'));
    if (!current_user_can('edit_post', $post_id)) {
        wp_die(-1);
    }
    $wp_list_table->prepare_items();
    if (!$wp_list_table->has_items()) {
        wp_die(1);
    }
    $x = new WP_Ajax_Response();
    ob_start();
    foreach ($wp_list_table->items as $comment) {
        if (!current_user_can('edit_comment', $comment->comment_ID)) {
            continue;
        }
        get_comment($comment);
        $wp_list_table->single_row($comment);
    }
    $comment_list_item = ob_get_contents();
    ob_end_clean();
    $x->add(array('what' => 'comments', 'data' => $comment_list_item));
    $x->send();
}