wp_attachment_is

Verifies an attachment is of a given type.

Description

wp_attachment_is( (string) $type, (null) $post = null ); 

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

  1. if ( !function_exists( 'wp_attachment_is' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Attachment type. Accepts 'image', 'audio', or 'video'. 
  5. $type = ''; 
  6.  
  7. // Optional. Attachment ID or object. Default is global $post. 
  8. $post = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_attachment_is($type, $post); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function wp_attachment_is( $type, $post = null ) { 
  2. if ( ! $post = get_post( $post ) ) { 
  3. return false; 
  4.  
  5. if ( ! $file = get_attached_file( $post->ID ) ) { 
  6. return false; 
  7.  
  8. if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) { 
  9. return true; 
  10.  
  11. $check = wp_check_filetype( $file ); 
  12. if ( empty( $check['ext'] ) ) { 
  13. return false; 
  14.  
  15. $ext = $check['ext']; 
  16.  
  17. if ( 'import' !== $post->post_mime_type ) { 
  18. return $type === $ext; 
  19.  
  20. switch ( $type ) { 
  21. case 'image': 
  22. $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); 
  23. return in_array( $ext, $image_exts ); 
  24.  
  25. case 'audio': 
  26. return in_array( $ext, wp_get_audio_extensions() ); 
  27.  
  28. case 'video': 
  29. return in_array( $ext, wp_get_video_extensions() ); 
  30.  
  31. default: 
  32. return $type === $ext;