WordPress.org

Make WordPress Core

Changeset 46553


Ignore:
Timestamp:
10/15/2019 07:10:53 PM (5 weeks ago)
Author:
azaozz
Message:

Media: Similarly to wp_get_original_image_path() add wp_get_original_image_url() to always retrieve the URL to the original uploaded image.

Props kraftbj.
Fixes #48302.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r46451 r46553  
    71817181    return apply_filters( 'wp_get_original_image_path', $original_image, $attachment_id );
    71827182}
     7183
     7184/**
     7185 * Retrieve the URL to an original attachment image.
     7186 *
     7187 * Similar to `wp_get_attachment_url()` however some images may have been
     7188 * processed after uploading. In this case this function returns the URL
     7189 * to the originally uploaded image file.
     7190 *
     7191 * @since 5.3.0
     7192 *
     7193 * @param int $attachment_id Attachment post ID.
     7194 * @return string|false Attachment image URL, false on error or if the attachment is not an image.
     7195 */
     7196function wp_get_original_image_url( $attachment_id ) {
     7197    if ( ! wp_attachment_is_image( $attachment_id ) ) {
     7198        return false;
     7199    }
     7200
     7201    $image_url = wp_get_attachment_url( $attachment_id );
     7202
     7203    if ( empty( $image_url ) ) {
     7204        return false;
     7205    }
     7206
     7207    $image_meta = wp_get_attachment_metadata( $attachment_id );
     7208
     7209    if ( empty( $image_meta['original_image'] ) ) {
     7210        $original_image_url = $image_url;
     7211    } else {
     7212        $original_image_url = path_join( dirname( $image_url ), $image_meta['original_image'] );
     7213    }
     7214
     7215    /**
     7216     * Filters the URL to the original attachment image.
     7217     *
     7218     * @since 5.3.0
     7219     *
     7220     * @param string $original_image_url URL to original image.
     7221     * @param int    $attachment_id      Attachment ID.
     7222     */
     7223    return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id );
     7224}
Note: See TracChangeset for help on using the changeset viewer.