yit_plugin_get_attachment_id

Return the ID of an attachment.

Description

(int) yit_plugin_get_attachment_id( (string) $url ); 

Returns (int)

Parameters (1)

0. $url (string)
The url.

Usage

  1. if ( !function_exists( 'yit_plugin_get_attachment_id' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'yith-woocommerce-compare/plugin-fw/yit-functions.php'; 
  3.  
  4. // The url. 
  5. $url = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = yit_plugin_get_attachment_id($url); 
  9.  

Defined (1)

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

/plugin-fw/yit-functions.php  
  1. function yit_plugin_get_attachment_id ( $url ) { 
  2.  
  3. $upload_dir = wp_upload_dir (); 
  4. $dir = trailingslashit ( $upload_dir[ 'baseurl' ] ); 
  5.  
  6. if ( false === strpos ( $url, $dir ) ) { 
  7. return false; 
  8.  
  9. $file = basename ( $url ); 
  10.  
  11. $query = array ( 
  12. 'post_type' => 'attachment',  
  13. 'fields' => 'ids',  
  14. 'meta_query' => array ( 
  15. array ( 
  16. 'value' => $file,  
  17. 'compare' => 'LIKE',  
  18. ),  
  19. ),  
  20. ); 
  21.  
  22. $query[ 'meta_query' ][ 0 ][ 'key' ] = '_wp_attached_file'; 
  23. $ids = get_posts ( $query ); 
  24.  
  25. foreach ( $ids as $id ) { 
  26. $attachment_image = wp_get_attachment_image_src ( $id, 'full' ); 
  27. if ( $url == array_shift ( $attachment_image ) || $url == str_replace ( 'https://', 'http://', array_shift ( $attachment_image ) ) ) { 
  28. return $id; 
  29. $query[ 'meta_query' ][ 0 ][ 'key' ] = '_wp_attachment_metadata'; 
  30. $ids = get_posts ( $query ); 
  31.  
  32. foreach ( $ids as $id ) { 
  33.  
  34. $meta = wp_get_attachment_metadata ( $id ); 
  35. if ( ! isset( $meta[ 'sizes' ] ) ) { 
  36. continue; 
  37.  
  38. foreach ( (array)$meta[ 'sizes' ] as $size => $values ) { 
  39. if ( $values[ 'file' ] == $file && $url == str_replace ( 'https://', 'http://', array_shift ( wp_get_attachment_image_src ( $id, $size ) ) ) ) { 
  40.  
  41. return $id; 
  42.  
  43. return false;