WordPress.org

Make WordPress Core

Ticket #19629: wordpress_38938.diff

File wordpress_38938.diff, 1.5 KB (added by dotancohen, 4 years ago)

Patch to optionally return the ID of a sideloaded media file.

  • src/wp-admin/includes/media.php

    diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php
    index c6b2dcd..de937cf 100644
    a b function wp_media_upload_handler() { 
    848848 * @param string $file    The URL of the image to download.
    849849 * @param int    $post_id The post ID the media is to be associated with.
    850850 * @param string $desc    Optional. Description of the image.
    851  * @param string $return  Optional. Accepts 'html' (image tag html) or 'src' (URL). Default 'html'.
     851 * @param string $return  Optional. Accepts 'html' (image tag html), 'id' (Media ID), or 'src' (URL). Default 'html'.
    852852 * @return string|WP_Error Populated HTML img tag on success, WP_Error object otherwise.
    853853 */
    854854function media_sideload_image( $file, $post_id, $desc = null, $return = 'html' ) {
    function media_sideload_image( $file, $post_id, $desc = null, $return = 'html' ) 
    884884        }
    885885
    886886        // Finally, check to make sure the file has been saved, then return the HTML.
    887         if ( ! empty( $src ) ) {
    888                 if ( $return === 'src' ) {
    889                         return $src;
    890                 }
    891 
    892                 $alt = isset( $desc ) ? esc_attr( $desc ) : '';
    893                 $html = "<img src='$src' alt='$alt' />";
    894                 return $html;
    895         } else {
     887        if ( empty( $src ) ) {
    896888                return new WP_Error( 'image_sideload_failed' );
    897889        }
     890
     891        if ( $return === 'src' ) {
     892                return $src;
     893        }
     894
     895        if ( $return === 'id' ) {
     896                return $id;
     897        }
     898
     899        $alt = isset( $desc ) ? esc_attr( $desc ) : '';
     900        $html = "<img src='$src' alt='$alt' />";
     901        return $html;
    898902}
    899903
    900904/**