get_image_tag

Gets an img tag for an image attachment, scaling it down if requested.

Description

(string) get_image_tag( (int) $id, (string) $alt, (string) $title, (string) $align, (string) $size = 'medium' ); 

The filter allows for changing the class name for the image without having to use regular expressions on the HTML content. The parameters are: what WordPress will use for the class, the Attachment ID, image align value, and the size the image should be.

The second filter, , has the HTML content, which can then be further manipulated by a plugin to change all attribute values and even HTML content.

Returns (string)

HTML IMG element for given image attachment

Parameters (5)

0. $id (int)
Attachment ID.
1. $alt (string)
Image Description for the alt attribute.
2. $title (string)
Image Description for the title attribute.
3. $align (string)
Part of the class name for aligning the image.
4. $size — Optional. (string) => 'medium'
Registered image size to retrieve a tag for. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default medium..

Usage

  1. if ( !function_exists( 'get_image_tag' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Attachment ID. 
  5. $id = -1; 
  6.  
  7. // Image Description for the alt attribute. 
  8. $alt = ''; 
  9.  
  10. // Image Description for the title attribute. 
  11. $title = ''; 
  12.  
  13. // Part of the class name for aligning the image. 
  14. $align = ''; 
  15. $size = 'medium'; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = get_image_tag($id, $alt, $title, $align, $size); 
  19.  

Defined (1)

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

/wp-includes/media.php  
  1. function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { 
  2.  
  3. list( $img_src, $width, $height ) = image_downsize($id, $size); 
  4. $hwstring = image_hwstring($width, $height); 
  5.  
  6. $title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; 
  7.  
  8. $class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id; 
  9.  
  10. /** 
  11. * Filters the value of the attachment's image tag class attribute. 
  12. * @since 2.6.0 
  13. * @param string $class CSS class name or space-separated list of classes. 
  14. * @param int $id Attachment ID. 
  15. * @param string $align Part of the class name for aligning the image. 
  16. * @param string|array $size Size of image. Image size or array of width and height values (in that order). 
  17. * Default 'medium'. 
  18. */ 
  19. $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size ); 
  20.  
  21. $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" ' . $title . $hwstring . 'class="' . $class . '" />'; 
  22.  
  23. /** 
  24. * Filters the HTML content for the image tag. 
  25. * @since 2.6.0 
  26. * @param string $html HTML content for the image. 
  27. * @param int $id Attachment ID. 
  28. * @param string $alt Alternate text. 
  29. * @param string $title Attachment title. 
  30. * @param string $align Part of the class name for aligning the image. 
  31. * @param string|array $size Size of image. Image size or array of width and height values (in that order). 
  32. * Default 'medium'. 
  33. */ 
  34. return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );