media_handle_upload() – Saves a file submitted from a POST request and create an attachment post for it.

Description

Saves a file submitted from a POST request and create an attachment post for it.

Usage

$int|WP_Error = media_handle_upload( $file_id, $post_id, $post_data, $overrides );

Parameters

$file_id
( string ) required – Index of the $_FILES array that the file was sent.
$post_id
( int ) required – The post ID of a post to attach the media item to. Required, but can be set to 0, creating a media item that has no relationship to a post.
$post_data
( array ) optional – Optional. Overwrite some of the attachment.
$overrides
( array ) optional default: Array – Optional. Override the wp_handle_upload() behavior.

Returns

int|WP_Error ID of the attachment or a WP_Error object on failure.

Source

File name: wordpress/wp-admin/includes/media.php


Lines:

1 to 100 of 144
function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) {
  $time = current_time( 'mysql' );
  $post = get_post( $post_id );

  if ( $post ) {
    // The post date doesn't usually matter for pages, so don't backdate this upload.
    if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) {
      $time = $post->post_date;
    }
  }

  $file = wp_handle_upload( $_FILES[ $file_id ], $overrides, $time );

  if ( isset( $file['error'] ) ) {
    return new WP_Error( 'upload_error', $file['error'] );
  }

  $name = $_FILES[ $file_id ]['name'];
  $ext  = pathinfo( $name, PATHINFO_EXTENSION );
  $name = wp_basename( $name, ".$ext" );

  $url     = $file['url'];
  $type    = $file['type'];
  $file    = $file['file'];
  $title   = sanitize_text_field( $name );
  $content = '';
  $excerpt = '';

  if ( preg_match( '#^audio#', $type ) ) {
    $meta = wp_read_audio_metadata( $file );

    if ( ! empty( $meta['title'] ) ) {
      $title = $meta['title'];
    }

    if ( ! empty( $title ) ) {

      if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
        /* translators: 1: Audio track title, 2: Album title, 3: Artist name. */
        $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
      } elseif ( ! empty( $meta['album'] ) ) {
        /* translators: 1: Audio track title, 2: Album title. */
        $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
      } elseif ( ! empty( $meta['artist'] ) ) {
        /* translators: 1: Audio track title, 2: Artist name. */
        $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
      } else {
        /* translators: %s: Audio track title. */
        $content .= sprintf( __( '"%s".' ), $title );
      }
    } elseif ( ! empty( $meta['album'] ) ) {

      if ( ! empty( $meta['artist'] ) ) {
        /* translators: 1: Audio album title, 2: Artist name. */
        $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
      } else {
        $content .= $meta['album'] . '.';
      }
    } elseif ( ! empty( $meta['artist'] ) ) {

      $content .= $meta['artist'] . '.';

    }

    if ( ! empty( $meta['year'] ) ) {
      /* translators: Audio file track information. %d: Year of audio track release. */
      $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
    }

    if ( ! empty( $meta['track_number'] ) ) {
      $track_number = explode( '/', $meta['track_number'] );

      if ( is_numeric( $track_number[0] ) ) {
        if ( isset( $track_number[1] ) && is_numeric( $track_number[1] ) ) {
          $content .= ' ' . sprintf(
            /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
            __( 'Track %1$s of %2$s.' ),
            number_format_i18n( $track_number[0] ),
            number_format_i18n( $track_number[1] )
          );
        } else {
          $content .= ' ' . sprintf(
            /* translators: Audio file track information. %s: Audio track number. */
            __( 'Track %s.' ),
            number_format_i18n( $track_number[0] )
          );
        }
      }
    }

    if ( ! empty( $meta['genre'] ) ) {
      /* translators: Audio file genre information. %s: Audio genre name. */
      $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
    }

    // Use image exif/iptc data for title and caption defaults if possible.
  } elseif ( 0 === strpos( $type, 'image/' ) ) {
    $image_meta = wp_read_image_metadata( $file );

    if ( $image_meta ) {

 View on GitHub View on Trac

Called by

    Invoked by

      Calls

      Call hooks

      API Letters: ,,