Skip to content

Instantly share code, notes, and snippets.

@ajvillegas
Last active August 12, 2018 04:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ajvillegas/3c4afb3f7dda19c5fb6a93a5a3de2208 to your computer and use it in GitHub Desktop.
Save ajvillegas/3c4afb3f7dda19c5fb6a93a5a3de2208 to your computer and use it in GitHub Desktop.
Illustrates how to extract attachment details returned by wp_prepare_attachment_for_js().
<?php
/**
* Illustrates how to extract attachment details returned by wp_prepare_attachment_for_js().
*
* The wp_prepare_attachment_for_js() function returns an array with the attachment post object
* that can be used to extract specific information for the attachment.
*
* Following is a list of all the details that the function returns extrated into individual variables.
*
* @author Alexis J. Villegas <alexis@ajvillegas.com>
* @link https://developer.wordpress.org/reference/functions/wp_prepare_attachment_for_js/
*/
// Get attachment details ($attachment_id = attachment ID or object)
$attachment = wp_prepare_attachment_for_js( $attachment_id );
// Attachment ID
$id = $attachment['id'];
// Attachment title
$title = $attachment['title'];
// Attachment file name
$filename = $attachment['filename'];
// Attachment URL
$url = $attachment['url'];
// Link to attachment page
$link = $attachment['link'];
// Attachment alt text
$alt = $attachment['alt'];
// Attachment author ID
$author = $attachment['author'];
// Attachment description
$description = $attachment['description'];
// Attachment caption
$caption = $attachment['caption'];
// Attachment name (no file extension)
$name = $attachment['name'];
// Attachment post status
$status = $attachment['status'];
// Attachment post parent
$uploadedTo = $attachment['uploadedTo'];
// Date attachment was uploaded in Unix timestamp format
$date = $attachment['date'];
// Date attachment was last modified in Unix timestamp format
$modified = $attachment['modified'];
// Attachment menu order parameter
$menuOrder = $attachment['menuOrder'];
// Attachment MIME type (type/subtype)
$mime = $attachment['mime'];
// Attachment file type (e.g., image)
$type = $attachment['type'];
// Attachment file subtype (e.g., jpeg)
$subtype = $attachment['subtype'];
// Attachment icon URL
$icon = $attachment['icon'];
// Formatted date when attachment was uploaded
$dateFormatted = $attachment['dateFormatted'];
// An array containing each nonce
$nonces = $attachment['nonces'];
// Attachment edit link URL
$editLink = $attachment['editLink'];
// Attachment meta
$meta = $attachment['meta'];
// Attachment author name
$authorName = $attachment['authorName'];
// Attachment file size in bytes
$filesizeInBytes = $attachment['filesizeInBytes'];
// Attachment file size
$filesizeHumanReadable = $attachment['filesizeHumanReadable'];
// Attachment height (in pixels)
$height = $attachment['height'];
// Attachment width (in pixels)
$width = $attachment['width'];
// Attachment orientation
$orientation = $attachment['orientation'];
// An sub-array containing details for each size
$sizes = $attachment['sizes'];
// Attachment 'medium' size height (replace 'medium' with name of attachment size)
$size_medium_height = $attachment['sizes']['medium']['height'];
// Attachment 'medium' size width (replace 'medium' with name of attachment size)
$size_medium_width = $attachment['sizes']['medium']['width'];
// Attachment 'medium' size URL (replace 'medium' with name of attachment size)
$size_medium_url = $attachment['sizes']['medium']['url'];
// Attachment 'medium' size orientation (replace 'medium' with name of attachment size)
$size_medium_orientation = $attachment['sizes']['medium']['orientation'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment