^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数media_sideload_image()用法示例

media_sideload_image( string $file,  int $post_id,  string $desc = null,  string $return = 'html' )

Downloads an image from the specified URL and attaches it to a post.


描述


参数

$file

(string)
(Required)
The URL of the image to download.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

$post_id

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

$desc

(string)
(Optional)
描述 of the image.

Default value: null

$return

(string)
(Optional)
Accepts 'html' (image tag html) or 'src' (URL), or 'id' (attachment ID).

Default value: 'html'


返回值

(string|WP_Error) Populated HTML img tag on success, WP_Error object otherwise.


源代码

File: wp-admin/includes/media.php

function media_sideload_image( $file, $post_id, $desc = null, $return = 'html' ) {
	if ( ! empty( $file ) ) {

		// Set variables for storage, fix file filename for query strings.
		preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
		if ( ! $matches ) {
			return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL' ) );
		}

		$file_array = array();
		$file_array['name'] = basename( $matches[0] );

		// Download file to temp location.
		$file_array['tmp_name'] = download_url( $file );

		// If error storing temporarily, return the error.
		if ( is_wp_error( $file_array['tmp_name'] ) ) {
			return $file_array['tmp_name'];
		}

		// Do the validation and storage stuff.
		$id = media_handle_sideload( $file_array, $post_id, $desc );

		// If error storing permanently, unlink.
		if ( is_wp_error( $id ) ) {
			@unlink( $file_array['tmp_name'] );
			return $id;
		// If attachment id was requested, return it early.
		} elseif ( $return === 'id' ) {
			return $id;
		}

		$src = wp_get_attachment_url( $id );
	}

	// Finally, check to make sure the file has been saved, then return the HTML.
	if ( ! empty( $src ) ) {
		if ( $return === 'src' ) {
			return $src;
		}

		$alt = isset( $desc ) ? esc_attr( $desc ) : '';
		$html = "<img src='$src' alt='$alt' />";
		return $html;
	} else {
		return new WP_Error( 'image_sideload_failed' );
	}
}

更新日志

Version 描述
4.8.0 Introduced the 'id' option within the $return parameter.
4.2.0 Introduced the $return parameter.
2.6.0 Introduced.

相关函数

Uses

  • wp-admin/includes/media.php:
    media_handle_sideload()
  • wp-admin/includes/file.php:
    download_url()
  • wp-includes/l10n.php:
    __()
  • wp-includes/formatting.php:
    esc_attr()
  • wp-includes/post.php:
    wp_get_attachment_url()
  • wp-includes/load.php:
    is_wp_error()
  • wp-includes/class-wp-error.php:
    WP_Error::__construct()
  • Show 2 more uses


Used By

  • wp-admin/includes/class-wp-press-this.php:
    WP_Press_This::side_load_images()

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Default Usage

    
    $url     = "https://wordpress.org/about/images/logos/wordpress-logo-stacked-rgb.png";
    $post_id = 1;
    $desc    = "The WordPress Logo";
    
    $image = media_sideload_image( $url, $post_id, $desc );
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈