wp_ajax_set_post_thumbnail

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

WordPress Version: 6.3

/**
 * Handles setting the featured image via AJAX.
 *
 * @since 3.1.0
 */
function wp_ajax_set_post_thumbnail()
{
    $json = !empty($_REQUEST['json']);
    // New-style request.
    $post_id = (int) $_POST['post_id'];
    if (!current_user_can('edit_post', $post_id)) {
        wp_die(-1);
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if ($json) {
        check_ajax_referer("update-post_{$post_id}");
    } else {
        check_ajax_referer("set_post_thumbnail-{$post_id}");
    }
    if ('-1' == $thumbnail_id) {
        if (delete_post_thumbnail($post_id)) {
            $return = _wp_post_thumbnail_html(null, $post_id);
            $json ? wp_send_json_success($return) : wp_die($return);
        } else {
            wp_die(0);
        }
    }
    if (set_post_thumbnail($post_id, $thumbnail_id)) {
        $return = _wp_post_thumbnail_html($thumbnail_id, $post_id);
        $json ? wp_send_json_success($return) : wp_die($return);
    }
    wp_die(0);
}

WordPress Version: 6.2

/**
 * Ajax handler for setting the featured image.
 *
 * @since 3.1.0
 */
function wp_ajax_set_post_thumbnail()
{
    $json = !empty($_REQUEST['json']);
    // New-style request.
    $post_id = (int) $_POST['post_id'];
    if (!current_user_can('edit_post', $post_id)) {
        wp_die(-1);
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if ($json) {
        check_ajax_referer("update-post_{$post_id}");
    } else {
        check_ajax_referer("set_post_thumbnail-{$post_id}");
    }
    if ('-1' == $thumbnail_id) {
        if (delete_post_thumbnail($post_id)) {
            $return = _wp_post_thumbnail_html(null, $post_id);
            $json ? wp_send_json_success($return) : wp_die($return);
        } else {
            wp_die(0);
        }
    }
    if (set_post_thumbnail($post_id, $thumbnail_id)) {
        $return = _wp_post_thumbnail_html($thumbnail_id, $post_id);
        $json ? wp_send_json_success($return) : wp_die($return);
    }
    wp_die(0);
}

WordPress Version: 5.6

/**
 * Ajax handler for setting the featured image.
 *
 * @since 3.1.0
 */
function wp_ajax_set_post_thumbnail()
{
    $json = !empty($_REQUEST['json']);
    // New-style request.
    $post_ID = (int) $_POST['post_id'];
    if (!current_user_can('edit_post', $post_ID)) {
        wp_die(-1);
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if ($json) {
        check_ajax_referer("update-post_{$post_ID}");
    } else {
        check_ajax_referer("set_post_thumbnail-{$post_ID}");
    }
    if ('-1' == $thumbnail_id) {
        if (delete_post_thumbnail($post_ID)) {
            $return = _wp_post_thumbnail_html(null, $post_ID);
            $json ? wp_send_json_success($return) : wp_die($return);
        } else {
            wp_die(0);
        }
    }
    if (set_post_thumbnail($post_ID, $thumbnail_id)) {
        $return = _wp_post_thumbnail_html($thumbnail_id, $post_ID);
        $json ? wp_send_json_success($return) : wp_die($return);
    }
    wp_die(0);
}

WordPress Version: 5.4

/**
 * Ajax handler for setting the featured image.
 *
 * @since 3.1.0
 */
function wp_ajax_set_post_thumbnail()
{
    $json = !empty($_REQUEST['json']);
    // New-style request.
    $post_ID = intval($_POST['post_id']);
    if (!current_user_can('edit_post', $post_ID)) {
        wp_die(-1);
    }
    $thumbnail_id = intval($_POST['thumbnail_id']);
    if ($json) {
        check_ajax_referer("update-post_{$post_ID}");
    } else {
        check_ajax_referer("set_post_thumbnail-{$post_ID}");
    }
    if ('-1' == $thumbnail_id) {
        if (delete_post_thumbnail($post_ID)) {
            $return = _wp_post_thumbnail_html(null, $post_ID);
            $json ? wp_send_json_success($return) : wp_die($return);
        } else {
            wp_die(0);
        }
    }
    if (set_post_thumbnail($post_ID, $thumbnail_id)) {
        $return = _wp_post_thumbnail_html($thumbnail_id, $post_ID);
        $json ? wp_send_json_success($return) : wp_die($return);
    }
    wp_die(0);
}

WordPress Version: 4.0

/**
 * Ajax handler for setting the featured image.
 *
 * @since 3.1.0
 */
function wp_ajax_set_post_thumbnail()
{
    $json = !empty($_REQUEST['json']);
    // New-style request
    $post_ID = intval($_POST['post_id']);
    if (!current_user_can('edit_post', $post_ID)) {
        wp_die(-1);
    }
    $thumbnail_id = intval($_POST['thumbnail_id']);
    if ($json) {
        check_ajax_referer("update-post_{$post_ID}");
    } else {
        check_ajax_referer("set_post_thumbnail-{$post_ID}");
    }
    if ($thumbnail_id == '-1') {
        if (delete_post_thumbnail($post_ID)) {
            $return = _wp_post_thumbnail_html(null, $post_ID);
            $json ? wp_send_json_success($return) : wp_die($return);
        } else {
            wp_die(0);
        }
    }
    if (set_post_thumbnail($post_ID, $thumbnail_id)) {
        $return = _wp_post_thumbnail_html($thumbnail_id, $post_ID);
        $json ? wp_send_json_success($return) : wp_die($return);
    }
    wp_die(0);
}

WordPress Version: 3.7

function wp_ajax_set_post_thumbnail()
{
    $json = !empty($_REQUEST['json']);
    // New-style request
    $post_ID = intval($_POST['post_id']);
    if (!current_user_can('edit_post', $post_ID)) {
        wp_die(-1);
    }
    $thumbnail_id = intval($_POST['thumbnail_id']);
    if ($json) {
        check_ajax_referer("update-post_{$post_ID}");
    } else {
        check_ajax_referer("set_post_thumbnail-{$post_ID}");
    }
    if ($thumbnail_id == '-1') {
        if (delete_post_thumbnail($post_ID)) {
            $return = _wp_post_thumbnail_html(null, $post_ID);
            $json ? wp_send_json_success($return) : wp_die($return);
        } else {
            wp_die(0);
        }
    }
    if (set_post_thumbnail($post_ID, $thumbnail_id)) {
        $return = _wp_post_thumbnail_html($thumbnail_id, $post_ID);
        $json ? wp_send_json_success($return) : wp_die($return);
    }
    wp_die(0);
}