функция
since 4.2.0
wp_attachment_is()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_attachment_is( string $type, int|WP_Post $post = null ): bool
Описание
Проверяет, что вложение имеет заданный тип.
Параметры
$type
string
обязательный
Тип вложения. Принимает image, audio, video или расширение файла.
$post
int|WP_Post
необязательный
= null
ID или объект вложения. По умолчанию — глобальный $post.
Возвращаемое значение
bool
Исходный код
wp-includes/post.php:7110
function wp_attachment_is( $type, $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$file = get_attached_file( $post->ID );
if ( ! $file ) {
return false;
}
if ( str_starts_with( $post->post_mime_type, $type . '/' ) ) {
return true;
}
$check = wp_check_filetype( $file );
if ( empty( $check['ext'] ) ) {
return false;
}
$ext = $check['ext'];
if ( 'import' !== $post->post_mime_type ) {
return $type === $ext;
}
switch ( $type ) {
case 'image':
$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp', 'avif', 'heic' );
return in_array( $ext, $image_exts, true );
case 'audio':
return in_array( $ext, wp_get_audio_extensions(), true );
case 'video':
return in_array( $ext, wp_get_video_extensions(), true );
default:
return $type === $ext;
}
}
История изменений
| Версия | Описание |
|---|---|
| 4.2.0 | Introduced. |

