Skip to content

Instantly share code, notes, and snippets.

@yobabyte
Created June 15, 2024 04:01
Show Gist options
  • Save yobabyte/b4a0a0ffb5c3d885fa9410b3726062fe to your computer and use it in GitHub Desktop.
Save yobabyte/b4a0a0ffb5c3d885fa9410b3726062fe to your computer and use it in GitHub Desktop.
function wp_attachment_is($type, $post) {
$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 ($post->post_mime_type !== 'import' && $type === $ext) {
return true;
}
switch ($type) {
case 'image':
$image_exts = ['jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp', 'avif'];
return in_array($ext, $image_exts, true);
case 'audio':
$audio_exts = wp_get_audio_extensions();
return in_array($ext, $audio_exts, true);
case 'video':
$video_exts = wp_get_video_extensions();
return in_array($ext, $video_exts, true);
default:
return $type === $ext;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment