WordPress at a glance

wp_attachment_is() WP 1.0

Verifies an attachment is of a given type.

Is the basis for: wp_attachment_is_image()
✈ 1 time = 0.00137s = very slow | 50000 times = 11.36s = slow

No Hooks.

Return

true/false. True if one of the accepted types, false otherwise.

Usage

wp_attachment_is( $type, $post );
$type(string) (required)
Attachment type. Accepts 'image', 'audio', or 'video'.
$post(int/WP_Post)
Attachment ID or object.
Default: global $post

Code of wp_attachment_is: wp-includes/post.php VER 4.9.8

<?php
function wp_attachment_is( $type, $post = null ) {
	if ( ! $post = get_post( $post ) ) {
		return false;
	}

	if ( ! $file = get_attached_file( $post->ID ) ) {
		return false;
	}

	if ( 0 === strpos( $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' );
		return in_array( $ext, $image_exts );

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

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

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

Related Functions

From tag: Conditional tags (all)

More from category: Attachments (Media files)

No comments
    Hello, !     Log In . Register