Skip to content

Instantly share code, notes, and snippets.

@ryanshoover
Last active December 9, 2018 19:58
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 ryanshoover/537da5ff2e3382fbc946dad6618751b8 to your computer and use it in GitHub Desktop.
Save ryanshoover/537da5ff2e3382fbc946dad6618751b8 to your computer and use it in GitHub Desktop.
diff --git a/wp-admin/custom-header.php b/wp-admin/custom-header.php
index 9460c80ca6..dd7306e1c2 100644
--- a/wp-admin/custom-header.php
+++ b/wp-admin/custom-header.php
@@ -1461,12 +1461,10 @@ endif;
public function get_uploaded_header_images() {
$header_images = get_uploaded_header_images();
$timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
- $alt_text_key = '_wp_attachment_image_alt';
foreach ( $header_images as &$header_image ) {
- $header_meta = get_post_meta( $header_image['attachment_id'] );
- $header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
- $header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
+ $header_image['timestamp'] = get_post_meta( $header_image['attachment_id'], 'timestamp', true );
+ $header_image['alt_text'] = wp_get_attachment_image_attr( $header_image['attachment_id'], 'alt' );
}
return $header_images;
diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php
index 93eceeca3a..b7839f0525 100644
--- a/wp-admin/includes/media.php
+++ b/wp-admin/includes/media.php
@@ -1357,7 +1357,7 @@ function get_attachment_fields_to_edit( $post, $errors = null ) {
// This was formerly in image_attachment_fields_to_edit().
if ( substr( $post->post_mime_type, 0, 5 ) == 'image' ) {
- $alt = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
+ $alt = wp_get_attachment_image_attr( $post->ID, 'alt' );
if ( empty( $alt ) ) {
$alt = '';
}
diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php
index 434e3245a1..4dbc759c2f 100644
--- a/wp-includes/general-template.php
+++ b/wp-includes/general-template.php
@@ -905,7 +905,7 @@ function get_custom_logo( $blog_id = 0 ) {
* If the logo alt attribute is empty, get the site title and explicitly
* pass it to the attributes used by wp_get_attachment_image().
*/
- $image_alt = get_post_meta( $custom_logo_id, '_wp_attachment_image_alt', true );
+ $image_alt = wp_get_attachment_image_attr( $custom_logo_id, 'alt' );
if ( empty( $image_alt ) ) {
$custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' );
}
diff --git a/wp-includes/media.php b/wp-includes/media.php
index ec50703b33..fc100ecbdd 100644
--- a/wp-includes/media.php
+++ b/wp-includes/media.php
@@ -853,6 +853,170 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
}
+/**
+ * Retrieve an image attribute value.
+ *
+ * Retrieve either a single image attribute value or all relevant attribute values. The possible
+ * values are `alt`, `description`, `caption`, `src`, `width`, `height`, `srcset`, `sizes`, `class`. When
+ * retrieving src, width, height, srcset, sizes, or class, you can specify the size of the image to retrieve
+ * in the args parameter. Passing in an empty attribute value will return all relevant values.
+ *
+ * @since 5.X
+ *
+ * @param int $attachment_id Image attachment ID.
+ * @param string|array $attribute Image attribute to retrieve.
+ * @param array $args Optional. Array of attributes for the function. Currently only
+ * supports 'size' for the image source.
+ * @return string|array Returns the value of the attribute or attributes requested. A single
+ * attribute will return a single value. An array of attributes or no
+ * attribute will return an array of values.
+ */
+function wp_get_attachment_image_attr( $attachment_id, $attribute = [], $args = [] ) {
+ $image = get_post( $attachment_id );
+
+ // If this isn't an image, bail early with null.
+ if ( empty( $image ) ) {
+ return null;
+ }
+
+ $values = [];
+ $names = [];
+
+ if ( empty( $attribute ) ) {
+ // If an empty value is passed for the attr name, return all our attributes.
+ $names = array(
+ 'alt',
+ 'description',
+ 'caption',
+ 'src',
+ 'width',
+ 'height',
+ 'srcset',
+ 'sizes',
+ 'class',
+ );
+ } elseif ( is_string( $attribute ) ) {
+ // If the attribute name is a string, convert it to an array.
+ $names = array( $attribute );
+ }
+
+ // Get a valid args array.
+ $default_args = array(
+ 'size' => null,
+ );
+
+ $args = wp_parse_args( $args, $default_args );
+
+ // Loop through our names and get the value for each one.
+ foreach ( $names as $name ) {
+ switch ( $name ) {
+ case 'alt':
+ $values[ $name ] = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
+ break;
+
+ case 'description':
+ $values[ $name ] = $image->post_content;
+ break;
+
+ case 'caption':
+ $values[ $name ] = $image->post_excerpt;
+ break;
+
+ case 'src':
+ if ( ! isset( $image_src ) ) {
+ $image_src = image_downsize( $attachment_id, $args['size'] );
+ }
+
+ $values[ $name ] = $image_src[0];
+ break;
+
+ case 'width':
+ if ( ! isset( $image_src ) ) {
+ $image_src = image_downsize( $attachment_id, $args['size'] );
+ }
+
+ $values[ $name ] = $image_src[1];
+ break;
+
+ case 'height':
+ if ( ! isset( $image_src ) ) {
+ $image_src = image_downsize( $attachment_id, $args['size'] );
+ }
+
+ $values[ $name ] = $image_src[2];
+ break;
+
+ case 'srcset':
+ $values[ $name ] = wp_get_attachment_image_srcset( $attachment_id, $args['size'] );
+ break;
+
+ case 'sizes':
+ $values[ $name ] = wp_get_attachment_image_sizes( $attachment_id, $args['size'] );
+ break;
+
+ case 'class':
+ $size_class = $args['size'];
+
+ if ( is_array( $size_class ) ) {
+ $size_class = join( 'x', $size_class );
+ }
+
+ $values[ $name ] = 'attachment-' . $size_class . ' size-' . $size_class;
+ break;
+ }
+ }
+
+ /**
+ * Filters the image attr result.
+ *
+ * Will apply to all image attribute types.
+ *
+ * @since 5.x
+ *
+ * @param string|int $values Value of the attribute.
+ * @param int $attachment_id Image attachment ID.
+ * @param string $name Name of the attribute being retrieved.
+ * @param array $args Arguments passed into the function.
+ */
+ $values = apply_filters( 'wp_get_attachment_image_attr', $values, $attachment_id, $attribute, $args );
+
+ // If we didn't request a single attribute, we're done.
+ if ( is_array( $attribute ) || empty( $attribute ) ) {
+ return $values;
+ }
+
+ if ( ! empty( $values[ $attribute ] ) ) {
+ $value = $values[ $attribute ];
+ } else {
+ $value = null;
+ }
+
+ /**
+ * Filters the image attr result.
+ *
+ * This is a dynamic filter that will generate a distinct filter for each attribute type.
+ * It only runs if the requested attribute is a string.
+ *
+ * <code>
+ * <?php
+ * function filter_alt_text( $alt ) {
+ * $alt = $alt + ' Extra content';
+ * return $alt;
+ * }
+ * add_filter( 'wp_get_attachment_image_attr_alt', 'filter_alt_text' );
+ * </code>
+ *
+ * @since 5.x
+ *
+ * @param string|int $value Value of the attribute.
+ * @param int $attachment_id Image attachment ID.
+ * @param array $args Arguments passed into the function.
+ */
+ $value = apply_filters( 'wp_get_attachment_image_attr_' . $attribute, $value, $attachment_id, $args );
+
+ return $value;
+}
+
/**
* Get an HTML img element representing an image attachment
*
diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
index e317f523aa..424f066025 100644
--- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
+++ b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
@@ -308,7 +308,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
}
if ( in_array( 'alt_text', $fields, true ) ) {
- $data['alt_text'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
+ $data['alt_text'] = wp_get_attachment_image_attr( $post->ID, 'alt' );
}
if ( in_array( 'media_type', $fields, true ) ) {
diff --git a/wp-includes/theme.php b/wp-includes/theme.php
index 3d26800436..c45dfbdd40 100644
--- a/wp-includes/theme.php
+++ b/wp-includes/theme.php
@@ -1275,7 +1275,7 @@ function get_uploaded_header_images() {
$header_images[ $header_index ]['attachment_id'] = $header->ID;
$header_images[ $header_index ]['url'] = $url;
$header_images[ $header_index ]['thumbnail_url'] = $url;
- $header_images[ $header_index ]['alt_text'] = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );
+ $header_images[ $header_index ]['alt_text'] = wp_get_attachment_image_attr( $header->ID, 'alt' );
$header_images[ $header_index ]['attachment_parent'] = isset( $header_data['attachment_parent'] ) ? $header_data['attachment_parent'] : '';
if ( isset( $header_data['width'] ) ) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment