wp_attachment_is

The timeline below displays how wordpress function wp_attachment_is has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Verifies an attachment is of a given type.
 *
 * @since 4.2.0
 *
 * @param string      $type Attachment type. Accepts `image`, `audio`, `video`, or a file extension.
 * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
 * @return bool True if an accepted type or a matching file extension, false otherwise.
 */
function wp_attachment_is($type, $post = null)
{
    $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 ('import' !== $post->post_mime_type) {
        return $type === $ext;
    }
    switch ($type) {
        case 'image':
            $image_exts = array('jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp', 'avif');
            return in_array($ext, $image_exts, true);
        case 'audio':
            return in_array($ext, wp_get_audio_extensions(), true);
        case 'video':
            return in_array($ext, wp_get_video_extensions(), true);
        default:
            return $type === $ext;
    }
}

WordPress Version: 6.3

/**
 * Verifies an attachment is of a given type.
 *
 * @since 4.2.0
 *
 * @param string      $type Attachment type. Accepts 'image', 'audio', or 'video'.
 * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
 * @return bool True if one of the accepted types, false otherwise.
 */
function wp_attachment_is($type, $post = null)
{
    $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 ('import' !== $post->post_mime_type) {
        return $type === $ext;
    }
    switch ($type) {
        case 'image':
            $image_exts = array('jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp');
            return in_array($ext, $image_exts, true);
        case 'audio':
            return in_array($ext, wp_get_audio_extensions(), true);
        case 'video':
            return in_array($ext, wp_get_video_extensions(), true);
        default:
            return $type === $ext;
    }
}

WordPress Version: 5.8

/**
 * Verifies an attachment is of a given type.
 *
 * @since 4.2.0
 *
 * @param string      $type Attachment type. Accepts 'image', 'audio', or 'video'.
 * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
 * @return bool True if one of the accepted types, false otherwise.
 */
function wp_attachment_is($type, $post = null)
{
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    $file = get_attached_file($post->ID);
    if (!$file) {
        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', 'webp');
            return in_array($ext, $image_exts, true);
        case 'audio':
            return in_array($ext, wp_get_audio_extensions(), true);
        case 'video':
            return in_array($ext, wp_get_video_extensions(), true);
        default:
            return $type === $ext;
    }
}

WordPress Version: 5.5

/**
 * Verifies an attachment is of a given type.
 *
 * @since 4.2.0
 *
 * @param string      $type Attachment type. Accepts 'image', 'audio', or 'video'.
 * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
 * @return bool True if one of the accepted types, false otherwise.
 */
function wp_attachment_is($type, $post = null)
{
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    $file = get_attached_file($post->ID);
    if (!$file) {
        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, true);
        case 'audio':
            return in_array($ext, wp_get_audio_extensions(), true);
        case 'video':
            return in_array($ext, wp_get_video_extensions(), true);
        default:
            return $type === $ext;
    }
}

WordPress Version: 5.3

/**
 * Verifies an attachment is of a given type.
 *
 * @since 4.2.0
 *
 * @param string      $type Attachment type. Accepts 'image', 'audio', or 'video'.
 * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
 * @return bool True if one of the accepted types, false otherwise.
 */
function wp_attachment_is($type, $post = null)
{
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    $file = get_attached_file($post->ID);
    if (!$file) {
        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;
    }
}

WordPress Version: 4.6

/**
 * Verifies an attachment is of a given type.
 *
 * @since 4.2.0
 *
 * @param string      $type Attachment type. Accepts 'image', 'audio', or 'video'.
 * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
 * @return bool True if one of the accepted types, false otherwise.
 */
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;
    }
}

WordPress Version: 4.2

/**
 * Verifies an attachment is of a given type.
 *
 * @since 4.2.0
 *
 * @param string      $type    Attachment type. Accepts 'image', 'audio', or 'video'.
 * @param int|WP_Post $post_id Optional. Attachment ID. Default 0.
 * @return bool True if one of the accepted types, false otherwise.
 */
function wp_attachment_is($type, $post_id = 0)
{
    if (!$post = get_post($post_id)) {
        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;
    }
}