wp_media_upload_handler

函式
wp_media_upload_handler ( No parameters )

wp_media_upload_handler: 這個函式用來處理媒體檔案的上傳。它檢查該檔案是否是有效的媒體檔案,然後將其儲存到適當的位置。

處理上傳媒體的程序。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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( wp_basename( $src ) );
}
if ( $title && $src ) {
$html = "<a href='" . esc_url( $src ) . "'>$title</a>";
}
$type = 'file';
$ext = preg_replace( '/^.+?.([^.]+)$/', '$1', $src );
if ( $ext ) {
$ext_type = wp_ext2type( $ext );
if ( '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.
*
* Possible hook names include:
*
* - `audio_send_to_editor_url`
* - `file_send_to_editor_url`
* - `video_send_to_editor_url`
*
* @since 3.3.0
*
* @param string $html HTML markup sent to the editor.
* @param string $src Media source URL.
* @param string $title Media title.
*/
$html = apply_filters( "{$type}_send_to_editor_url", $html, sanitize_url( $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 source 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, sanitize_url( $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'] ) && 'type_url' === $_GET['tab'] ) {
$type = 'image';
if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ), true ) ) {
$type = $_GET['type'];
}
return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
}
return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
}
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( wp_basename( $src ) ); } if ( $title && $src ) { $html = "<a href='" . esc_url( $src ) . "'>$title</a>"; } $type = 'file'; $ext = preg_replace( '/^.+?.([^.]+)$/', '$1', $src ); if ( $ext ) { $ext_type = wp_ext2type( $ext ); if ( '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. * * Possible hook names include: * * - `audio_send_to_editor_url` * - `file_send_to_editor_url` * - `video_send_to_editor_url` * * @since 3.3.0 * * @param string $html HTML markup sent to the editor. * @param string $src Media source URL. * @param string $title Media title. */ $html = apply_filters( "{$type}_send_to_editor_url", $html, sanitize_url( $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 source 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, sanitize_url( $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'] ) && 'type_url' === $_GET['tab'] ) { $type = 'image'; if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ), true ) ) { $type = $_GET['type']; } return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id ); } return wp_iframe( 'media_upload_type_form', 'image', $errors, $id ); }
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( wp_basename( $src ) );
			}

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

			$type = 'file';
			$ext  = preg_replace( '/^.+?.([^.]+)$/', '$1', $src );

			if ( $ext ) {
				$ext_type = wp_ext2type( $ext );
				if ( '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.
			 *
			 * Possible hook names include:
			 *
			 *  - `audio_send_to_editor_url`
			 *  - `file_send_to_editor_url`
			 *  - `video_send_to_editor_url`
			 *
			 * @since 3.3.0
			 *
			 * @param string $html  HTML markup sent to the editor.
			 * @param string $src   Media source URL.
			 * @param string $title Media title.
			 */
			$html = apply_filters( "{$type}_send_to_editor_url", $html, sanitize_url( $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 source 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, sanitize_url( $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'] ) && 'type_url' === $_GET['tab'] ) {
		$type = 'image';

		if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ), true ) ) {
			$type = $_GET['type'];
		}

		return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
	}

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

常見問題

FAQs
檢視更多 >