wc_get_product_attachment_props

Gets data about an attachment, such as alt text and captions.

Description

(array) wc_get_product_attachment_props( (constant) $attachment_id = null, (bool) $product = false ); 

Returns (array)

Parameters (2)

0. $attachment_id — Optional. (constant) => null
The attachment id.
1. $product — Optional. (bool) => false
The product.

Usage

  1. if ( !function_exists( 'wc_get_product_attachment_props' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-product-functions.php'; 
  3.  
  4. // The attachment id. 
  5. $attachment_id = null; 
  6.  
  7. // The product. 
  8. $product = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_get_product_attachment_props($attachment_id, $product); 
  12.  

Defined (1)

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

/includes/wc-product-functions.php  
  1. function wc_get_product_attachment_props( $attachment_id = null, $product = false ) { 
  2. $props = array( 
  3. 'title' => '',  
  4. 'caption' => '',  
  5. 'url' => '',  
  6. 'alt' => '',  
  7. 'src' => '',  
  8. 'srcset' => false,  
  9. 'sizes' => false,  
  10. ); 
  11. if ( $attachment = get_post( $attachment_id ) ) { 
  12. $props['title'] = trim( strip_tags( $attachment->post_title ) ); 
  13. $props['caption'] = trim( strip_tags( $attachment->post_excerpt ) ); 
  14. $props['url'] = wp_get_attachment_url( $attachment_id ); 
  15. $props['alt'] = trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ); 
  16.  
  17. // Large version. 
  18. $src = wp_get_attachment_image_src( $attachment_id, 'full' ); 
  19. $props['full_src'] = $src[0]; 
  20. $props['full_src_w'] = $src[1]; 
  21. $props['full_src_h'] = $src[2]; 
  22.  
  23. // Thumbnail version. 
  24. $src = wp_get_attachment_image_src( $attachment_id, 'shop_thumbnail' ); 
  25. $props['thumb_src'] = $src[0]; 
  26. $props['thumb_src_w'] = $src[1]; 
  27. $props['thumb_src_h'] = $src[2]; 
  28.  
  29. // Image source. 
  30. $src = wp_get_attachment_image_src( $attachment_id, 'shop_single' ); 
  31. $props['src'] = $src[0]; 
  32. $props['src_w'] = $src[1]; 
  33. $props['src_h'] = $src[2]; 
  34. $props['srcset'] = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $attachment_id, 'shop_single' ) : false; 
  35. $props['sizes'] = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $attachment_id, 'shop_single' ) : false; 
  36.  
  37. // Alt text fallbacks 
  38. $props['alt'] = empty( $props['alt'] ) ? $props['caption'] : $props['alt']; 
  39. $props['alt'] = empty( $props['alt'] ) ? trim( strip_tags( $attachment->post_title ) ) : $props['alt']; 
  40. $props['alt'] = empty( $props['alt'] ) && $product ? trim( strip_tags( get_the_title( $product->ID ) ) ) : $props['alt']; 
  41. return $props;