• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress a2z

WordPress a2z

WordPress Dynamic API Reference

  • Home
  • Plugins
  • Themes
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / twenty_twenty_one_get_attachment_image_attributes() – Filters the list of attachment image attributes.

You appear to be a bot. Output may be restricted

Description

Filters the list of attachment image attributes.

Usage

$string[] = twenty_twenty_one_get_attachment_image_attributes( $attr, $attachment, $size );

Parameters

$attr
( string[] ) required – Array of attribute values for the image markup, keyed by attribute name. See wp_get_attachment_image().
$attachment
( WP_Post ) required – Image attachment post.
$size
( string|int[] ) required – Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).

Returns

string[] The filtered attributes for the image markup.

Source

File name: twentytwentyone/inc/template-functions.php
Lines:

1 to 33 of 33
function twenty_twenty_one_get_attachment_image_attributes( $attr, $attachment, $size ) {

  if ( is_admin() ) {
    return $attr;
  }

  if ( isset( $attr['class'] ) && false !== strpos( $attr['class'], 'custom-logo' ) ) {
    return $attr;
  }

  $width  = false;
  $height = false;

  if ( is_array( $size ) ) {
    $width  = (int) $size[0];
    $height = (int) $size[1];
  } elseif ( $attachment && is_object( $attachment ) && $attachment->ID ) {
    $meta = wp_get_attachment_metadata( $attachment->ID );
    if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) {
      $width  = (int) $meta['width'];
      $height = (int) $meta['height'];
    }
  }

  if ( $width && $height ) {

    // Add style.
    $attr['style'] = isset( $attr['style'] ) ? $attr['style'] : '';
    $attr['style'] = 'width:100%;height:' . round( 100 * $height / $width, 2 ) . '%;max-width:' . $width . 'px;' . $attr['style'];
  }

  return $attr;
}
 

 View on GitHub View on Trac

Published: 9th December 2020 | Last updated: 26th January 2022

Primary Sidebar

Information

Function name: twenty_twenty_one_get_attachment_image_attributes
Plugin ref: Twenty Twenty-One
Version: 1.7
Sourcefile: inc/template-functions.php
File ref: inc/template-functions.php
Deprecated?: No
API Letters: A,G,O,T

Footer

WordPress a2z
WordPress a2z
WordPress Dynamic API Reference
WordPress 6.1.1. PHP: 8.0.28
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  wp-a2z.org
© Copyright WordPress a2z 2014-2023. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites