# wp_ajax_set_attachment_thumbnail()

URL: https://chugunov.pro/api-wordpress/functions/wp_ajax_set_attachment_thumbnail/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 4.0.0.

## Сигнатура

```php
wp_ajax_set_attachment_thumbnail()
```

## Описание

См. alsoset_post_thumbnail()

## Исходный код

Файл: `wp-admin/includes/ajax-actions.php:2808`

```php
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();
}
```

## История изменений

- 4.0.0 — Introduced.

## Связанные

Использует: [`attachment_url_to_postid`](https://chugunov.pro/api-wordpress/functions/attachment_url_to_postid/), [`set_post_thumbnail`](https://chugunov.pro/api-wordpress/functions/set_post_thumbnail/), [`current_user_can`](https://chugunov.pro/api-wordpress/functions/current_user_can/), [`check_ajax_referer`](https://chugunov.pro/api-wordpress/functions/check_ajax_referer/), [`wp_send_json_error`](https://chugunov.pro/api-wordpress/functions/wp_send_json_error/), [`wp_send_json_success`](https://chugunov.pro/api-wordpress/functions/wp_send_json_success/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_ajax_set_attachment_thumbnail/
