# do_enclose()

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

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

## Сигнатура

```php
do_enclose( string|null $content, int|WP_Post $post ): void|false
```

## Описание

Не добавляет вложения (enclosures), которые уже были добавлены, и удаляет вложения, которых больше нет в записи. Вызывается вместе с pingback и trackback.

## Параметры

- `$content` `string|null` — обязательный. Содержимое записи. Если null, используется поле post_content из $post.
- `$post` `int|WP_Post` — обязательный. Идентификатор записи или объект записи.

## Возвращаемое значение

`void|false`

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

Файл: `wp-includes/functions.php:888`

```php
function do_enclose( $content, $post ) {
	global $wpdb;

	// @todo Tidy this code and make the debug code optional.
	require_once ABSPATH . WPINC . '/class-IXR.php';

	$post = get_post( $post );
	if ( ! $post ) {
		return false;
	}

	if ( null === $content ) {
		$content = $post->post_content;
	}

	$post_links = array();

	$pung = get_enclosed( $post->ID );

	$post_links_temp = wp_extract_urls( $content );

	foreach ( $pung as $link_test ) {
		// Link is no longer in post.
		if ( ! in_array( $link_test, $post_links_temp, true ) ) {
			$mids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like( $link_test ) . '%' ) );
			foreach ( $mids as $mid ) {
				delete_metadata_by_mid( 'post', $mid );
			}
		}
	}

	foreach ( (array) $post_links_temp as $link_test ) {
		// If we haven't pung it already.
		if ( ! in_array( $link_test, $pung, true ) ) {
			$test = parse_url( $link_test );
			if ( false === $test ) {
				continue;
			}
			if ( isset( $test['query'] ) ) {
				$post_links[] = $link_test;
			} elseif ( isset( $test['path'] ) && ( '/' !== $test['path'] ) && ( '' !== $test['path'] ) ) {
				$post_links[] = $link_test;
			}
		}
	}

	/**
	 * Filters the list of enclosure links before querying the database.
	 *
	 * Allows for the addition and/or removal of potential enclosures to save
	 * to postmeta before checking the database for existing enclosures.
	 *
	 * @since 4.4.0
	 *
	 * @param string[] $post_links An array of enclosure links.
	 * @param int      $post_id    Post ID.
	 */
	$post_links = apply_filters( 'enclosure_links', $post_links, $post->ID );

	foreach ( (array) $post_links as $url ) {
		$url = strip_fragment_from_url( $url );

		if ( '' !== $url && ! $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like( $url ) . '%' ) ) ) {

			$headers = wp_get_http_headers( $url );
			if ( $headers ) {
				$len           = (int) ( $headers['Content-Length'] ?? 0 );
				$type          = $headers['Content-Type'] ?? '';
				$allowed_types = array( 'video', 'audio' );

				// Check to see if we can figure out the mime type from the extension.
				$url_parts = parse_url( $url );
				if ( false !== $url_parts && ! empty( $url_parts['path'] ) ) {
					$extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
					if ( ! empty( $extension ) ) {
						foreach ( wp_get_mime_types() as $exts => $mime ) {
							if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
								$type = $mime;
								break;
							}
						}
					}
				}

				if ( in_array( substr( $type, 0, strpos( $type, '/' ) ), $allowed_types, true ) ) {
					add_post_meta( $post->ID, 'enclosure', "$url\n$len\n$mime\n" );
				}
			}
		}
	}
}
```

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

- 5.6.0 — The $content parameter is no longer optional, but passing null to skip it is still supported.
- 5.3.0 — The $content parameter was made optional, and the $post parameter was updated to accept a post ID or a WP_Post object.
- 1.5.0 — Introduced.

## Связанные

Использует: [`strip_fragment_from_url`](https://chugunov.pro/api-wordpress/functions/strip_fragment_from_url/), `wpdb::esc_like`, [`wp_get_mime_types`](https://chugunov.pro/api-wordpress/functions/wp_get_mime_types/), [`wp_extract_urls`](https://chugunov.pro/api-wordpress/functions/wp_extract_urls/), [`wp_get_http_headers`](https://chugunov.pro/api-wordpress/functions/wp_get_http_headers/), [`get_enclosed`](https://chugunov.pro/api-wordpress/functions/get_enclosed/), [`add_post_meta`](https://chugunov.pro/api-wordpress/functions/add_post_meta/), `wpdb::get_col`, [`delete_metadata_by_mid`](https://chugunov.pro/api-wordpress/functions/delete_metadata_by_mid/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`get_post`](https://chugunov.pro/api-wordpress/functions/get_post/), `wpdb::get_var`, `wpdb::prepare`.
Используется в: [`do_all_enclosures`](https://chugunov.pro/api-wordpress/functions/do_all_enclosures/).

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