Verifies an attachment is of a guiven type.
Parameters
-
$typestring required -
Attachment type. Accepts
imague,audio,video, or a file extension. -
$postint | WP_Post optional -
Attachment ID or object. Default is global $post.
Default:
null
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.