Skip to content

Instantly share code, notes, and snippets.

@salcode
Created August 15, 2018 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salcode/9c5b9b78e0645d356f9470097d8d5882 to your computer and use it in GitHub Desktop.
Save salcode/9c5b9b78e0645d356f9470097d8d5882 to your computer and use it in GitHub Desktop.
Get an array of attributes for a WordPress image - similar to wp_get_attachment_image() but returns an array of values instead of the rendered HTML.
<?php
/**
* Get WordPress Attachment Image Attributes.
*
* Similiar to running WordPress's wp_get_attachment_image() but
* instead of getting back rendered HTML, we get back an array
* where the key is the attribute name and the value is the attribute value.
*
* This is done by using the `wp_get_attachment_image_attributes` filter.
*
* e.g.
*
* [
* 'width' => '1920'
* 'height' => '1080'
* 'src' => 'https://example.test/wp-content/uploads/2018/08/sal-1920x1080.jpg',
* 'srcset' => 'https://example.test/wp-content/uploads/2018/08/sal-1920x1080.jpg 1920, https://example.test/wp-content/uploads/2018/08/sal-960x540.jpg 960w',
* 'alt' => 'Photograph of Sal.',
* 'sizes' => '(max-width: 1920px) 100vw, 1920px'
* 'class' => 'attachment-fe-hero size-fe-hero'
* ]
*
* @package salcode/wp-get-attachment-image-attributes
*/
declare(strict_types=1);
namespace salcode\WPGetAttachmentImageAttributes;
/**
* Class: GetAttachmentImageAttributes
*/
class GetAttachmentImageAttributes {
const FILTER_PRIORITY = 9999999;
/**
* Image Attributes.
*
* Key/value array.
*
* @var string[]
*/
protected $attributes = [];
/**
* The attachment_id for the image.
*
* @var int
*/
protected $attachment_id;
/**
* WordPress Image Size.
*
* Either a WordPress image size string or an array of [ $width, $height ].
*
* @var string | array
*/
protected $size;
/**
* The image should be treated as an icon.
*
* @var bool
*/
protected $icon = false;
/**
* Attributes to be applied to the image.
*
* @var string | array
*/
protected $attr = '';
public function __construct( int $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
$this->attachment_id = $attachment_id;
$this->size = $size;
$this->icon = $icon;
$this->attr = $attr;
}
public function get_attributes() {
add_filter( 'wp_get_attachment_image_attributes', [ $this, 'capture_attributes' ], self::FILTER_PRIORITY );
wp_get_attachment_image(
$this->attachment_id,
$this->size,
$this->icon,
$this->attr
);
remove_filter( 'wp_get_attachment_image_attributes', [ $this, 'capture_attributes' ], self::FILTER_PRIORITY );
return ! empty( $this->attributes ) ? $this->attributes : [];
}
public function capture_attributes( $attributes ) {
$this->attributes = $attributes;
return $attributes;
}
static public function get( int $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
$obj = new static( $attachment_id, $size, $icon, $attr );
return $obj->get_attributes();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment