Guest User

Untitled

a guest
Mar 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. function find_image_post_id($url) {
  2. global $wpdb;
  3. $postid = $wpdb->get_var($wpdb->prepare("SELECT DISTINCT ID FROM $wpdb->posts WHERE guid='$url'"));
  4. if ($postid) {
  5. return $postid;
  6. }
  7. return false;
  8. }
  9.  
  10. if ( ! function_exists( 'get_attachment_id' ) ) {
  11. /**
  12. * Get the Attachment ID for a given image URL.
  13. *
  14. * @link http://wordpress.stackexchange.com/a/7094
  15. *
  16. * @param string $url
  17. *
  18. * @return boolean|integer
  19. */
  20. function get_attachment_id( $url ) {
  21.  
  22. $dir = wp_upload_dir();
  23.  
  24. // baseurl never has a trailing slash
  25. if ( false === strpos( $url, $dir['baseurl'] . '/' ) ) {
  26. // URL points to a place outside of upload directory
  27. return false;
  28. }
  29.  
  30. $file = basename( $url );
  31. $query = array(
  32. 'post_type' => 'attachment',
  33. 'fields' => 'ids',
  34. 'meta_query' => array(
  35. array(
  36. 'key' => '_wp_attached_file',
  37. 'value' => $file,
  38. 'compare' => 'LIKE',
  39. ),
  40. )
  41. );
  42.  
  43. // query attachments
  44. $ids = get_posts( $query );
  45.  
  46. if ( ! empty( $ids ) ) {
  47.  
  48. foreach ( $ids as $id ) {
  49.  
  50. // first entry of returned array is the URL
  51. if ( $url === array_shift( wp_get_attachment_image_src( $id, 'full' ) ) )
  52. return $id;
  53. }
  54. }
  55.  
  56. $query['meta_query'][0]['key'] = '_wp_attachment_metadata';
  57.  
  58. // query attachments again
  59. $ids = get_posts( $query );
  60.  
  61. if ( empty( $ids) )
  62. return false;
  63.  
  64. foreach ( $ids as $id ) {
  65.  
  66. $meta = wp_get_attachment_metadata( $id );
  67.  
  68. foreach ( $meta['sizes'] as $size => $values ) {
  69.  
  70. if ( $values['file'] === $file && $url === array_shift( wp_get_attachment_image_src( $id, $size ) ) )
  71. return $id;
  72. }
  73. }
  74.  
  75. return false;
  76. }
  77. }
  78.  
  79. $attachment_id = attachment_url_to_postid( $image_url );
  80. echo $attachment_id;
  81.  
  82. function get_attachment_id( $url, $ignore_path = false ) {
  83.  
  84. if ( ! $ignore_path ) {
  85.  
  86. $dir = wp_upload_dir();
  87. $dir = trailingslashit($dir['baseurl']);
  88.  
  89. if( false === strpos( $url, $dir ) )
  90. return false;
  91. }
  92.  
  93. $file = basename($url);
  94.  
  95. $query = array(
  96. 'post_type' => 'attachment',
  97. 'fields' => 'ids',
  98. 'meta_query' => array(
  99. array(
  100. 'key' => '_wp_attached_file',
  101. 'value' => $file,
  102. 'compare' => 'LIKE',
  103. )
  104. )
  105. );
  106.  
  107. $ids = get_posts( $query );
  108.  
  109. foreach( $ids as $id ) {
  110. $match = array_shift( wp_get_attachment_image_src($id, 'full') );
  111. if( $url == $match || ( $ignore_path && strstr( $match, $file ) ) )
  112. return $id;
  113. }
  114.  
  115. $query['meta_query'][0]['key'] = '_wp_attachment_metadata';
  116. $ids = get_posts( $query );
  117.  
  118. foreach( $ids as $id ) {
  119.  
  120. $meta = wp_get_attachment_metadata($id);
  121.  
  122. foreach( $meta['sizes'] as $size => $values ) {
  123. if( $values['file'] == $file && ( $ignore_path || $url == array_shift( wp_get_attachment_image_src($id, $size) ) ) )
  124. return $id;
  125. }
  126. }
  127.  
  128. return false;
  129. }
  130.  
  131. // This is getting the image / url
  132. $feature1 = get_theme_mod('feature_image_1');
  133.  
  134. // This is getting the post id
  135. $feature1_id = attachment_url_to_postid($feature1);
  136.  
  137. // This is getting the alt text from the image that is set in the media area
  138. $image1_alt = get_post_meta( $feature1_id, '_wp_attachment_image_alt', true );
  139.  
  140. <a href="<?php echo $feature1_url; ?>"><img class="img-responsive center-block" src="<?php echo $feature1; ?>" alt="<?php echo $image1_alt; ?>"></a>
  141.  
  142. $image_url = get_field('main_image'); // in case of custom field usage
  143. $image_id = attachment_url_to_postid($image_url);
  144.  
  145. // retrieve the thumbnail size of our image
  146. $image_thumb = wp_get_attachment_image_src($image_id, 'thumbnail');
Add Comment
Please, Sign In to add comment