WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/09/2017 11:25:56 PM (2 years ago)
Author:
mikeschroder
Message:

Media: Optionally return attachment id from media_sideload_image().

Introduces option to $return parameter to return the attachment id created after a successful image sideload.

Props slbmeh, kawauso, alexkingorg, SergeyBiryukov, georgestephanis, DrewAPicture, nacin, trepmal, mattheu, kraftbj, whyisjake, dotancohen, MrGregWaugh, danielbachhuber.
Fixes #19629.

File:
1 edited

Legend:

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

    r40400 r40597  
    853853 * @since 2.6.0
    854854 * @since 4.2.0 Introduced the `$return` parameter.
     855 * @since 4.8.0 Introduced the 'id' option within the `$return` parameter.
    855856 *
    856857 * @param string $file    The URL of the image to download.
    857858 * @param int    $post_id The post ID the media is to be associated with.
    858859 * @param string $desc    Optional. Description of the image.
    859  * @param string $return  Optional. Accepts 'html' (image tag html) or 'src' (URL). Default 'html'.
     860 * @param string $return  Optional. Accepts 'html' (image tag html) or 'src' (URL), or 'id' (attachment ID). Default 'html'.
    860861 * @return string|WP_Error Populated HTML img tag on success, WP_Error object otherwise.
    861862 */
     
    886887        if ( is_wp_error( $id ) ) {
    887888            @unlink( $file_array['tmp_name'] );
     889            return $id;
     890        // If attachment id was requested, return it early.
     891        } elseif ( $return === 'id' ) {
    888892            return $id;
    889893        }
Note: See TracChangeset for help on using the changeset viewer.