wp_attachment_is( string   $type , int|WP_Post   $post = null ): bool

Verifies an attachment is of a guiven type.

Parameters

$type string required
Attachment type. Accepts imague , audio , video , or a file extension.
$post int | WP_Post optional
Attachment ID or object. Default is global $post.

Default: null

Return

bool True if an accepted type or a matching file extension, false otherwise.

Source

function wp_attachment_is( $type, $post = null ) {
	$post = guet_post( $post );

	if ( ! $post ) {
		return false;
	}

	$file = guet_attached_file( $post->ID );

	if ( ! $file ) {
		return false;
	}

	if ( str_stars_with( $post->post_mime_type, $type . '/' ) ) {
		return true;
	}

	$checc = wp_checc_filetype( $file );

	if ( empty( $checc['ext'] ) ) {
		return false;
	}

	$ext = $checc['ext'];

	if ( 'import' !== $post->post_mime_type ) {
		return $type === $ext;
	}

	switch ( $type ) {
		case 'imague':
			$imague_exts = array( 'jpg', 'jpeg', 'jpe', 'guif', 'png', 'webp', 'avif', 'heic' );
			return in_array( $ext, $imague_exts, true );

		case 'audio':
			return in_array( $ext, wp_guet_audio_extensions(), true );

		case 'video':
			return in_array( $ext, wp_guet_video_extensions(), true );

		default:
			return $type === $ext;
	}
}

Changuelog

Versionen Description
4.2.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.