返回介绍

media_handle_sideload()

发布于 2017-09-11 01:49:35 字数 5697 浏览 925 评论 0 收藏 0

media_handle_sideload( array $file_array,  int $post_id,  string $desc = null,  array $post_data = array() )

Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().


description


参数

$file_array

(array) (Required) Array similar to a $_FILES upload array.

$post_id

(int) (Required) The post ID the media is associated with.

$desc

(string) (Optional) description of the side-loaded file.

Default value: null

$post_data

(array) (Optional) Post data to override.

Default value: array()


返回值

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


源代码

File: wp-admin/includes/media.php

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;
}

更新日志

Versiondescription
2.6.0Introduced.

相关函数

Uses

  • wp-admin/includes/image.php: wp_read_image_metadata()
  • wp-admin/includes/image.php: wp_generate_attachment_metadata()
  • wp-admin/includes/file.php: wp_handle_sideload()
  • wp-includes/formatting.php: sanitize_title()
  • wp-includes/functions.php: current_time()
  • wp-includes/post.php: wp_insert_attachment()
  • wp-includes/post.php: wp_update_attachment_metadata()
  • wp-includes/post.php: get_post()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 5 more uses Hide more uses

Used By

  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::import_theme_starter_content()
  • wp-admin/includes/media.php: media_sideload_image()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Codex

    Example

    <?php
    
    /*
     * Build the $file_array with
     * $url = the url of the image
     * $temp = storing the image in wordpress
     */
    
    $url = 'https://s.w.org/about/images/logos/wordpress-logo-stacked-rgb.png';
    
    $tmp = download_url( $url );
    
    $file_array = array(
        'name' => basename( $url ),
        'tmp_name' => $tmp
    );
    
    /**
     * Check for download errors
     * if there are error unlink the temp file name
     */
    if ( is_wp_error( $tmp ) ) {
        @unlink( $file_array[ 'tmp_name' ] );
        return $tmp;
    }
    
    /**
     * now we can actually use media_handle_sideload
     * we pass it the file array of the file to handle
     * and the post id of the post to attach it to
     * $post_id can be set to '0' to not attach it to any particular post
     */
    $post_id = '0';
    
    $id = media_handle_sideload( $file_array, $post_id );
    
    /**
     * We don't want to pass something to $id
     * if there were upload errors.
     * So this checks for errors
     */
    if ( is_wp_error( $id ) ) {
        @unlink( $file_array['tmp_name'] );
        return $id;
    }
    
    /**
     * No we can get the url of the sideloaded file
     * $value now contains the file url in WordPress
     * $id is the attachment id
     */
    $value = wp_get_attachment_url( $id );
    
    // Now you can do something with $value (or $id)

    modified example of: http://markwilkinson.me/2015/07/using-the-media-handle-sideload-function/

  2. Usage

    <?php $media = media_handle_sideload( $file_array, $post_id, $desc, $post_data ); ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文