返回介绍

wp_media_upload_handler()

发布于 2017-09-11 12:27:23 字数 5203 浏览 972 评论 0 收藏 0

wp_media_upload_handler()

Handles the process of uploading media.


description


返回值

(null|string)


源代码

File: wp-admin/includes/media.php

function wp_media_upload_handler() {
	$errors = array();
	$id = 0;

	if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
		check_admin_referer('media-form');
		// Upload File button was clicked
		$id = media_handle_upload('async-upload', $_REQUEST['post_id']);
		unset($_FILES);
		if ( is_wp_error($id) ) {
			$errors['upload_error'] = $id;
			$id = false;
		}
	}

	if ( !empty($_POST['insertonlybutton']) ) {
		$src = $_POST['src'];
		if ( !empty($src) && !strpos($src, '://') )
			$src = "http://$src";

		if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
			$title = esc_html( wp_unslash( $_POST['title'] ) );
			if ( empty( $title ) )
				$title = esc_html( basename( $src ) );

			if ( $title && $src )
				$html = "<a href='" . esc_url($src) . "'>$title</a>";

			$type = 'file';
			if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
				&& ( 'audio' == $ext_type || 'video' == $ext_type ) )
					$type = $ext_type;

			/**
			 * Filters the URL sent to the editor for a specific media type.
			 *
			 * The dynamic portion of the hook name, `$type`, refers to the type
			 * of media being sent.
			 *
			 * @since 3.3.0
			 *
			 * @param string $html  HTML markup sent to the editor.
			 * @param string $src   Media 源代码 URL.
			 * @param string $title Media title.
			 */
			$html = apply_filters( "{$type}_send_to_editor_url", $html, esc_url_raw( $src ), $title );
		} else {
			$align = '';
			$alt = esc_attr( wp_unslash( $_POST['alt'] ) );
			if ( isset($_POST['align']) ) {
				$align = esc_attr( wp_unslash( $_POST['align'] ) );
				$class = " class='align$align'";
			}
			if ( !empty($src) )
				$html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";

			/**
			 * Filters the image URL sent to the editor.
			 *
			 * @since 2.8.0
			 *
			 * @param string $html  HTML markup sent to the editor for an image.
			 * @param string $src   Image 源代码 URL.
			 * @param string $alt   Image alternate, or alt, text.
			 * @param string $align The image alignment. Default 'alignnone'. Possible values include
			 *                      'alignleft', 'aligncenter', 'alignright', 'alignnone'.
			 */
			$html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
		}

		return media_send_to_editor($html);
	}

	if ( isset( $_POST['save'] ) ) {
		$errors['upload_notice'] = __('Saved.');
		wp_enqueue_script( 'admin-gallery' );
 		return wp_iframe( 'media_upload_gallery_form', $errors );

	} elseif ( ! empty( $_POST ) ) {
		$return = media_upload_form_handler();

		if ( is_string($return) )
			return $return;
		if ( is_array($return) )
			$errors = $return;
	}

	if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
		$type = 'image';
		if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
			$type = $_GET['type'];
		return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
	}

	return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-admin/includes/media.php: media_upload_form_handler()
  • wp-admin/includes/media.php: {$type}_send_to_editor_url
  • wp-admin/includes/media.php: image_send_to_editor_url
  • wp-admin/includes/media.php: media_handle_upload()
  • wp-admin/includes/media.php: media_send_to_editor()
  • wp-admin/includes/media.php: wp_iframe()
  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: wp_unslash()
  • wp-includes/formatting.php: esc_html()
  • wp-includes/formatting.php: esc_url()
  • wp-includes/formatting.php: esc_url_raw()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/pluggable.php: check_admin_referer()
  • wp-includes/functions.wp-scripts.php: wp_enqueue_script()
  • wp-includes/functions.php: wp_ext2type()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/load.php: is_wp_error()
  • Show 12 more uses Hide more uses

Used By

  • wp-admin/includes/deprecated.php: media_upload_image()
  • wp-admin/includes/deprecated.php: media_upload_audio()
  • wp-admin/includes/deprecated.php: media_upload_video()
  • wp-admin/includes/deprecated.php: media_upload_file()

User Contributed Notes

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

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

发布评论

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