ControlledChaos
4/4/2018 - 6:25 PM

Illustrates how to extract attachment details returned by wp_prepare_attachment_for_js().

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'];