wp_ajax_set_attachment_thumbnail

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

WordPress Version: 6.4

/**
 * Handles setting the featured image for an attachment via AJAX.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            ++$success;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 6.3

/**
 * Handles setting the featured image for an attachment via AJAX.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 2.1

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 6.2

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 1.2

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 6.1

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 9.6

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 5.9

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 8.7

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 5.8

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 7.9

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 7.2

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .10

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 6.2

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .11

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 5.2

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .12

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 4.2

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .13

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 3.2

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .15

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 2.3

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .20

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 2.2

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .18

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 1.2

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .16

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 0.3

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .20

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 0.2

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .19

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 9.3

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .23

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 6.3

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .26

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 4.6

/**
 * Ajax handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 5.4

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .30

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 5.3

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .29

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 4.4

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .30

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 3.4

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .31

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 2.4

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .35

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 1.5

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .40

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 1.4

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: .38

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    if (false === check_ajax_referer('set-attachment-thumbnail', '_ajax_nonce', false)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}

WordPress Version: 4.0

/**
 * AJAX handler for setting the featured image for an attachment.
 *
 * @since 4.0.0
 *
 * @see set_post_thumbnail()
 */
function wp_ajax_set_attachment_thumbnail()
{
    if (empty($_POST['urls']) || !is_array($_POST['urls'])) {
        wp_send_json_error();
    }
    $thumbnail_id = (int) $_POST['thumbnail_id'];
    if (empty($thumbnail_id)) {
        wp_send_json_error();
    }
    $post_ids = array();
    // For each URL, try to find its corresponding post ID.
    foreach ($_POST['urls'] as $url) {
        $post_id = attachment_url_to_postid($url);
        if (!empty($post_id)) {
            $post_ids[] = $post_id;
        }
    }
    if (empty($post_ids)) {
        wp_send_json_error();
    }
    $success = 0;
    // For each found attachment, set its thumbnail.
    foreach ($post_ids as $post_id) {
        if (!current_user_can('edit_post', $post_id)) {
            continue;
        }
        if (set_post_thumbnail($post_id, $thumbnail_id)) {
            $success++;
        }
    }
    if (0 === $success) {
        wp_send_json_error();
    } else {
        wp_send_json_success();
    }
    wp_send_json_error();
}