Search code examples
phpwordpressmime-typesmime

If statement for wordpress MIME type post attachment


I am trying to create a if else statement for checking if the attachment is an audio file or not.

Could anyone assist in this please. I am using the wordpress get_post_mime_type function

if (get_post_mime_type($post->ID) == 'audio/mpeg') { 
    echo 'this is audio';
}
else {
    echo 'this aint audio';
}

Any help would be greatly appreciated. This is inside the wordpress loop.


Solution

  • Try this one:

    Its working in mine.

    $attachment_mime = wp_check_filetype(wp_get_attachment_url($post->ID) );
    
    if ( $attachment_mime['type'] == 'audio/mpeg') { 
        echo 'this is audio';
    }
    else {
        echo 'this aint audio';
    }
    

    Thanks.