Skip to content

Available Functions for Ver. 2 and below

Ankit Pokhrel edited this page Feb 17, 2014 · 3 revisions

The List of Available Helper Functions are:

  1. dfi_get_image_id( $image_url )
  2. dfi_get_image_thumb( $image_url, $size = "thumbnail" )
  3. dfi_get_image_url( $attachment_id, $size = "full" )
  4. dfi_get_post_attachment_ids( $post_id )
  5. dfi_is_attached( $attachment_id, $post_id )
  6. dfi_get_image_title( $image_url )
  7. dfi_get_image_title_by_id( $attachment_id )
  8. dfi_get_image_alt( $image_url )
  9. dfi_get_image_alt_by_id( $attachment_id )
  10. dfi_get_image_caption( $image_url )
  11. dfi_get_image_caption_by_id( $attachment_id )

1. dfi_get_image_id( $image_url )

Gets the attachment id of the 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 = dfi_get_image_id( "http://example.com/wp/wp-content/uploads/2013/10/image.jpg" ); 
//returns attachment id for this image

2. dfi_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 = dfi_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. dfi_get_image_url( $attachment_id, $size = "full" )

Fetches the image thumbnail 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 = dfi_get_image_thumb( 57, "thumbnail" ); 
//output: http://example.com/wp/wp-content/uploads/2013/10/image-150x150.jpg

4. dfi_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= dfi_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= dfi_get_post_attachment_ids( the_ID() ); 

5. dfi_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

dfi_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. dfi_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 = dfi_get_image_title( "http://example.com/wp/wp-content/uploads/2013/10/image.jpg" ); 
//Output: The image title

7. dfi_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 = dfi_get_image_title_by_id( 57 ); 
//Output: The image title

8. dfi_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 = dfi_get_image_alt( "http://example.com/wp/wp-content/uploads/2013/10/image.jpg" ); 
//Output: The image alternate text

9. dfi_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 = dfi_get_image_alt_by_id( 57 ); 
//Output: The image alternate text

10. dfi_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 = dfi_get_image_caption( "http://example.com/wp/wp-content/uploads/2013/10/image.jpg" ); 
//Output: The image caption

11. dfi_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 = dfi_get_image_caption_by_id( 57 ); 
//Output: The image caption

Links