Skip to content
Gleb Kemarsky edited this page Oct 10, 2019 · 7 revisions

The List of Available Helper Functions are:

  1. get_image_id( $image_url )
  2. get_image_thumb( $image_url, $size = "thumbnail" )
  3. get_image_url( $attachment_id, $size = "full" )
  4. get_post_attachment_ids( $post_id )
  5. is_attached( $attachment_id, $post_id )
  6. get_image_title( $image_url )
  7. get_image_title_by_id( $attachment_id )
  8. get_image_alt( $image_url )
  9. get_image_alt_by_id( $attachment_id )
  10. get_image_caption( $image_url )
  11. get_image_caption_by_id( $attachment_id )
  12. get_image_description( $image_url )
  13. get_image_description_by_id( $attachment_id )
  14. get_nth_featured_image( $position, $post_id )
  15. get_all_featured_images( $post_id )
  16. get_featured_images( $post_id )
  17. get_link_to_image( $attachment_id )

1. get_image_id( $image_url )

Gets attachment id of an image by its URL.

Params

  • $image_url (String) - The URL of the full size image whose attachment_id is required.

This function returns attachment_id on success and null on failure.

Usage

$attachment_id = $dynamic_featured_image->get_image_id( "http://example.com/wp/wp-content/uploads/2013/10/image.jpg" ); 

//returns attachment id for this image

2. get_image_thumb( $image_url, $size = "thumbnail" )

Fetches the image thumbnail url of specific size.

Params

  • $image_url (String) - The URL of the full size image.
  • $size(String) - The required size of the image. The available values are thumbnail (Default), medium, large and full.

This function returns Image URL on success and null on failure.

Usage

$thumbnail = $dynamic_featured_image->get_image_thumb( "http://example.com/wp/wp-content/uploads/2013/10/image.jpg", "thumbnail" ); 

//output: http://example.com/wp/wp-content/uploads/2013/10/image-150x150.jpg

3. get_image_url( $attachment_id, $size = "full" )

Fetches the image url of specific size by attachment id.

Params

  • $attachment_id (Integer) - The attachment id of the image.
  • $size(String) - The required size of the image. The available values are thumbnail (Default), medium, large and full.

This function returns Image URL on success and null on failure.

Usage

$thumbnail = $dynamic_featured_image->get_image_url( 57, "thumbnail" ); 

//output: http://example.com/wp/wp-content/uploads/2013/10/image-150x150.jpg

4. get_post_attachment_ids( $post_id )

Fetches all attachment ids of the image attached to this particular post.

Params

  • $post_id (Integer) - The id of the post to retrieve data from.

This function returns array of ids on success and empty array on failure.

Usage

$attachment_ids = $dynamic_featured_image->get_post_attachment_ids( 57 ); 
//output: array(0 => "5", 1 => "7")

//You can get the attachment ids inside the post loop by using something like this
$attachment_ids= $dynamic_featured_image->get_post_attachment_ids( get_the_ID() ); 

5. is_attached( $attachment_id, $post_id )

Checks if the image is attached with the particular post.

Params

  • $attachment_id (Integer) - The attachment id of the image.
  • $post_id (Integer) - The id of the post to check.

This function returns true (boolean) if the image is attached with the post otherwise false.

Usage

$dynamic_featured_image->is_attached( 57, 7 ); 

//output: true

Getting Image title, alt and caption attributes

When you add an image to media library, you'll have the option to enter a title, alt text and a caption.

title, alt, caption

Title is primarily for back end use, this holds the name of your image. Giving each image a human-readable title may pay dividends when your Media Library is much larger.

Alt text is descriptive text that appears when you hold your mouse over an image in a web page (or when the image does not appear). Alt text is important for web site visitors with visual or audio impairments who might use special readers to explore your site, and may also be used by search engines indexing your site.

The caption can be displayed below the image in the viewer that appears when a visitor clicks on your gallery image to zoom.

DFI provides following set of functions for interaction with these fields.

6. get_image_title( $image_url )

Fetches the title for particular image.

Params

  • $image_url (String) - The URL of the full size image whose attachment_id is required.

This function returns image title (String) on success and null on failure.

Usage

$title = $dynamic_featured_image->get_image_title( "http://example.com/wp/wp-content/uploads/2013/10/image.jpg" ); 

//Output: The image title

7. get_image_title_by_id( $attachment_id )

Fetches the title for particular image by attachment id.

Params

  • $attachment_id (Integer) - The attachment id of the image.

This function returns image title (String) on success and null on failure.

Usage

$title = $dynamic_featured_image->get_image_title_by_id( 57 ); 

//Output: The image title

8. get_image_alt( $image_url )

Fetches the alternate text for particular image.

Params

  • $image_url (String) - The URL of the full size image whose attachment_id is required.

This function returns alternate text/alt (String) on success and null on failure.

Usage

$alt = $dynamic_featured_image->get_image_alt( "http://example.com/wp/wp-content/uploads/2013/10/image.jpg" ); 

//Output: The image alternate text

9. get_image_alt_by_id( $attachment_id )

Fetches the alternate text for particular image by attachment id.

Params

  • $attachment_id (Integer) - The attachment id of the image.

This function returns image aternate text (String) on success and null on failure.

Usage

$alt = $dynamic_featured_image->get_image_alt_by_id( 57 ); 

//Output: The image alternate text

10. get_image_caption( $image_url )

Fetches the caption for particular image.

Params

  • $image_url (String) - The URL of the full size image whose attachment_id is required.

This function returns image caption (String) on success and null on failure.

Usage

$caption = $dynamic_featured_image->get_image_caption( "http://example.com/wp/wp-content/uploads/2013/10/image.jpg" ); 

//Output: The image caption

11. get_image_caption_by_id( $attachment_id )

Fetches the caption for particular image by attachment id.

Params

  • $attachment_id (Integer) - The attachment id of the image.

This function returns image caption (String) on success and null on failure.

Usage

$caption = $dynamic_featured_image->get_image_caption_by_id( 57 ); 

//Output: The image caption

12. get_image_description( $image_url )

Fetches the caption for particular image.

Params

  • $image_url (String) - The URL of the full size image whose attachment_id is required.

This function returns image description (String) on success and null on failure.

Usage

$description = $dynamic_featured_image->get_image_description( "http://example.com/wp/wp-content/uploads/2013/10/image.jpg" ); 

//Output: The image description

13. get_image_description_by_id( $attachment_id )

Fetches the caption for particular image by attachment id.

Params

  • $attachment_id (Integer) - The attachment id of the image.

This function returns image description (String) on success and null on failure.

Usage

$description = $dynamic_featured_image->get_image_description_by_id( 57 ); 

//Output: The image description

14. get_nth_featured_image( $position, $post_id = null )

Fetches information of nth featured image of a particular post.

Params

  • $position (Integer) - nth image which information is needed. Starts from 2.
  • $post_id (Integer) - id of the post to fetch the information from.

This function returns image data (Array) on success and null on failure.

15. get_all_featured_images( $post_id )

Fetches all featured images including the default WordPress one from particular post.

Params

  • $post_id (Integer) - id of the post to fetch the information from.

This function returns image data (Array) on success and empty array on failure.

Usage

//get all featured images from 5th post
$all_images = $dynamic_featured_image->get_all_featured_images( 5 );
 
//Output: Array with image data

16. get_featured_images( $post_id )

Fetches only featured images from particular post. Use get_all_featured_images() to include the default WordPress image too.

Params

  • $post_id (Integer) - id of the post to fetch the information from.

This function returns image data (Array) on success and empty array on failure.

17. get_link_to_image( $attachment_id )

Fetches link to image of a given attachment.

Params

  • $attachment_id (Integer) - attachment_id of the media to fetch the information from.

This function returns image data (String) on success and empty array on failure.

Links