wp_attachment_is
Verifies an attachment is of a given type.
Description
Parameters (2)
- 0. $type (string)
- Attachment type. Accepts image,, audio., or video.
- 1. $post — Optional. (null) =>
null
- Attachment ID or object. Default is global
$post
.
Usage
if ( !function_exists( 'wp_attachment_is' ) ) { require_once ABSPATH . WPINC . '/post.php'; } // Attachment type. Accepts 'image', 'audio', or 'video'. $type = ''; // Optional. Attachment ID or object. Default is global $post. $post = null; // NOTICE! Understand what this does before running. $result = wp_attachment_is($type, $post);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/post.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;
- }
- }