media_handle_sideload

The timeline below displays how wordpress function media_handle_sideload has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.7

/**
 * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
 *
 * @since 2.6.0
 * @since 5.3.0 The `$post_id` parameter was made optional.
 *
 * @param string[] $file_array Array that represents a `$_FILES` upload array.
 * @param int      $post_id    Optional. The post ID the media is associated with.
 * @param string   $desc       Optional. Description of the side-loaded file. Default null.
 * @param array    $post_data  Optional. Post data to override. Default empty array.
 * @return int|WP_Error The ID of the attachment or a WP_Error on failure.
 */
function media_handle_sideload($file_array, $post_id = 0, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    if (isset($post_data['post_date']) && substr($post_data['post_date'], 0, 4) > 0) {
        $time = $post_data['post_date'];
    } else {
        $post = get_post($post_id);
        if ($post && substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        } else {
            $time = current_time('mysql');
        }
    }
    $file = wp_handle_sideload($file_array, $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\.[^.]+$/', '', wp_basename($file));
    $content = '';
    // Use image exif/iptc data for title and caption defaults if possible.
    $image_meta = wp_read_image_metadata($file);
    if ($image_meta) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the attachment metadata.
    $attachment_id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($attachment_id)) {
        wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
    }
    return $attachment_id;
}

WordPress Version: 5.4

/**
 * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
 *
 * @since 2.6.0
 * @since 5.3.0 The `$post_id` parameter was made optional.
 *
 * @param array  $file_array Array similar to a `$_FILES` upload array.
 * @param int    $post_id    Optional. The post ID the media is associated with.
 * @param string $desc       Optional. Description of the side-loaded file. Default null.
 * @param array  $post_data  Optional. Post data to override. Default empty array.
 * @return int|WP_Error The ID of the attachment or a WP_Error on failure.
 */
function media_handle_sideload($file_array, $post_id = 0, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    $time = current_time('mysql');
    $post = get_post($post_id);
    if ($post) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_sideload($file_array, $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\.[^.]+$/', '', wp_basename($file));
    $content = '';
    // Use image exif/iptc data for title and caption defaults if possible.
    $image_meta = wp_read_image_metadata($file);
    if ($image_meta) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the attachment metadata.
    $attachment_id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($attachment_id)) {
        wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
    }
    return $attachment_id;
}

WordPress Version: 5.3

/**
 * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
 *
 * @since 2.6.0
 * @since 5.3.0 The `$post_id` parameter was made optional.
 *
 * @param array  $file_array Array similar to a `$_FILES` upload array.
 * @param int    $post_id    Optional. The post ID the media is associated with.
 * @param string $desc       Optional. Description of the side-loaded file. Default null.
 * @param array  $post_data  Optional. Post data to override. Default empty array.
 * @return int|WP_Error The ID of the attachment or a WP_Error on failure.
 */
function media_handle_sideload($file_array, $post_id = 0, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    $time = current_time('mysql');
    $post = get_post($post_id);
    if ($post) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_sideload($file_array, $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\.[^.]+$/', '', wp_basename($file));
    $content = '';
    // Use image exif/iptc data for title and caption defaults if possible.
    $image_meta = wp_read_image_metadata($file);
    if ($image_meta) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the attachment metadata
    $attachment_id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($attachment_id)) {
        wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
    }
    return $attachment_id;
}

WordPress Version: 5.2

/**
 * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
 *
 * @since 2.6.0
 *
 * @param array  $file_array Array similar to a `$_FILES` upload array.
 * @param int    $post_id    The post ID the media is associated with.
 * @param string $desc       Optional. Description of the side-loaded file. Default null.
 * @param array  $post_data  Optional. Post data to override. Default empty array.
 * @return int|object The ID of the attachment or a WP_Error on failure.
 */
function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_sideload($file_array, $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\.[^.]+$/', '', wp_basename($file));
    $content = '';
    // Use image exif/iptc data for title and caption defaults if possible.
    if ($image_meta = wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the attachment metadata
    $id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 5.1

/**
 * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
 *
 * @since 2.6.0
 *
 * @param array  $file_array Array similar to a `$_FILES` upload array.
 * @param int    $post_id    The post ID the media is associated with.
 * @param string $desc       Optional. Description of the side-loaded file. Default null.
 * @param array  $post_data  Optional. Post data to override. Default empty array.
 * @return int|object The ID of the attachment or a WP_Error on failure.
 */
function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_sideload($file_array, $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\.[^.]+$/', '', basename($file));
    $content = '';
    // Use image exif/iptc data for title and caption defaults if possible.
    if ($image_meta = wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the attachment metadata
    $id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .10

/**
 * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
 *
 * @since 2.6.0
 *
 * @param array  $file_array Array similar to a `$_FILES` upload array.
 * @param int    $post_id    The post ID the media is associated with.
 * @param string $desc       Optional. Description of the side-loaded file. Default null.
 * @param array  $post_data  Optional. Post data to override. Default empty array.
 * @return int|object The ID of the attachment or a WP_Error on failure.
 */
function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_sideload($file_array, $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\.[^.]+$/', '', basename($file));
    $content = '';
    // Use image exif/iptc data for title and caption defaults if possible.
    if ($image_meta = wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the attachment metadata
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.6

/**
 * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
 *
 * @since 2.6.0
 *
 * @param array  $file_array Array similar to a `$_FILES` upload array.
 * @param int    $post_id    The post ID the media is associated with.
 * @param string $desc       Optional. Description of the side-loaded file. Default null.
 * @param array  $post_data  Optional. Post data to override. Default empty array.
 * @return int|object The ID of the attachment or a WP_Error on failure.
 */
function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_sideload($file_array, $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\.[^.]+$/', '', basename($file));
    $content = '';
    // Use image exif/iptc data for title and caption defaults if possible.
    if ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the attachment metadata
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.3

/**
 * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
 *
 * @since 2.6.0
 *
 * @param array $file_array Array similar to a {@link $_FILES} upload array
 * @param int $post_id The post ID the media is associated with
 * @param string $desc Description of the sideloaded file
 * @param array $post_data allows you to overwrite some of the attachment
 * @return int|object The ID of the attachment or a WP_Error on failure
 */
function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_sideload($file_array, $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\.[^.]+$/', '', basename($file));
    $content = '';
    // Use image exif/iptc data for title and caption defaults if possible.
    if ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the attachment metadata
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.0

/**
 * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
 *
 * @since 2.6.0
 *
 * @param array $file_array Array similar to a {@link $_FILES} upload array
 * @param int $post_id The post ID the media is associated with
 * @param string $desc Description of the sideloaded file
 * @param array $post_data allows you to overwrite some of the attachment
 * @return int|object The ID of the attachment or a WP_Error on failure
 */
function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_sideload($file_array, $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\.[^.]+$/', '', basename($file));
    $content = '';
    // Use image exif/iptc data for title and caption defaults if possible.
    if ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the attachment metadata
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 3.7

/**
 * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
 *
 * @since 2.6.0
 *
 * @param array $file_array Array similar to a {@link $_FILES} upload array
 * @param int $post_id The post ID the media is associated with
 * @param string $desc Description of the sideloaded file
 * @param array $post_data allows you to overwrite some of the attachment
 * @return int|object The ID of the attachment or a WP_Error on failure
 */
function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_sideload($file_array, $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\.[^.]+$/', '', basename($file));
    $content = '';
    // use image exif/iptc data for title and caption defaults if possible
    if ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the attachment metadata
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}