media_upload_form_handler

函数
media_upload_form_handler ( No parameters )
返回值
  • (null|array|void) Array of error messages keyed by attachment ID, null or void on success.
定义位置
相关方法
wp_media_upload_handlermedia_upload_headermedia_upload_formmedia_upload_gallerymedia_upload_file
引入
2.5.0
弃用
-

media_upload_form_handler函数是一个WordPress动作,当媒体上传表单被提交时被调用。开发人员可以使用这个动作来处理上传的媒体文件,并在媒体库中创建新的附件。

处理传统媒体上传器的表单提交。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function media_upload_form_handler() {
check_admin_referer( 'media-form' );
$errors = null;
if ( isset( $_POST['send'] ) ) {
$keys = array_keys( $_POST['send'] );
$send_id = (int) reset( $keys );
}
if ( ! empty( $_POST['attachments'] ) ) {
foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
$post = get_post( $attachment_id, ARRAY_A );
$_post = $post;
if ( ! current_user_can( 'edit_post', $attachment_id ) ) {
continue;
}
if ( isset( $attachment['post_content'] ) ) {
$post['post_content'] = $attachment['post_content'];
}
if ( isset( $attachment['post_title'] ) ) {
$post['post_title'] = $attachment['post_title'];
}
if ( isset( $attachment['post_excerpt'] ) ) {
$post['post_excerpt'] = $attachment['post_excerpt'];
}
if ( isset( $attachment['menu_order'] ) ) {
$post['menu_order'] = $attachment['menu_order'];
}
if ( isset( $send_id ) && $attachment_id == $send_id ) {
if ( isset( $attachment['post_parent'] ) ) {
$post['post_parent'] = $attachment['post_parent'];
}
}
/**
* Filters the attachment fields to be saved.
*
* @since 2.5.0
*
* @see wp_get_attachment_metadata()
*
* @param array $post An array of post data.
* @param array $attachment An array of attachment metadata.
*/
$post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
if ( isset( $attachment['image_alt'] ) ) {
$image_alt = wp_unslash( $attachment['image_alt'] );
if ( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) !== $image_alt ) {
$image_alt = wp_strip_all_tags( $image_alt, true );
// update_post_meta() expects slashed.
update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
}
}
if ( isset( $post['errors'] ) ) {
$errors[ $attachment_id ] = $post['errors'];
unset( $post['errors'] );
}
if ( $post != $_post ) {
wp_update_post( $post );
}
foreach ( get_attachment_taxonomies( $post ) as $t ) {
if ( isset( $attachment[ $t ] ) ) {
wp_set_object_terms( $attachment_id, array_map( 'trim', preg_split( '/,+/', $attachment[ $t ] ) ), $t, false );
}
}
}
}
if ( isset( $_POST['insert-gallery'] ) || isset( $_POST['update-gallery'] ) ) {
?>
<script type="text/javascript">
var win = window.dialogArguments || opener || parent || top;
win.tb_remove();
</script>
<?php
exit;
}
if ( isset( $send_id ) ) {
$attachment = wp_unslash( $_POST['attachments'][ $send_id ] );
$html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
if ( ! empty( $attachment['url'] ) ) {
$rel = '';
if ( strpos( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) == $attachment['url'] ) {
$rel = " rel='attachment wp-att-" . esc_attr( $send_id ) . "'";
}
$html = "<a href='{$attachment['url']}'$rel>$html</a>";
}
/**
* Filters the HTML markup for a media item sent to the editor.
*
* @since 2.5.0
*
* @see wp_get_attachment_metadata()
*
* @param string $html HTML markup for a media item sent to the editor.
* @param int $send_id The first key from the $_POST['send'] data.
* @param array $attachment Array of attachment metadata.
*/
$html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
return media_send_to_editor( $html );
}
return $errors;
}
function media_upload_form_handler() { check_admin_referer( 'media-form' ); $errors = null; if ( isset( $_POST['send'] ) ) { $keys = array_keys( $_POST['send'] ); $send_id = (int) reset( $keys ); } if ( ! empty( $_POST['attachments'] ) ) { foreach ( $_POST['attachments'] as $attachment_id => $attachment ) { $post = get_post( $attachment_id, ARRAY_A ); $_post = $post; if ( ! current_user_can( 'edit_post', $attachment_id ) ) { continue; } if ( isset( $attachment['post_content'] ) ) { $post['post_content'] = $attachment['post_content']; } if ( isset( $attachment['post_title'] ) ) { $post['post_title'] = $attachment['post_title']; } if ( isset( $attachment['post_excerpt'] ) ) { $post['post_excerpt'] = $attachment['post_excerpt']; } if ( isset( $attachment['menu_order'] ) ) { $post['menu_order'] = $attachment['menu_order']; } if ( isset( $send_id ) && $attachment_id == $send_id ) { if ( isset( $attachment['post_parent'] ) ) { $post['post_parent'] = $attachment['post_parent']; } } /** * Filters the attachment fields to be saved. * * @since 2.5.0 * * @see wp_get_attachment_metadata() * * @param array $post An array of post data. * @param array $attachment An array of attachment metadata. */ $post = apply_filters( 'attachment_fields_to_save', $post, $attachment ); if ( isset( $attachment['image_alt'] ) ) { $image_alt = wp_unslash( $attachment['image_alt'] ); if ( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) !== $image_alt ) { $image_alt = wp_strip_all_tags( $image_alt, true ); // update_post_meta() expects slashed. update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); } } if ( isset( $post['errors'] ) ) { $errors[ $attachment_id ] = $post['errors']; unset( $post['errors'] ); } if ( $post != $_post ) { wp_update_post( $post ); } foreach ( get_attachment_taxonomies( $post ) as $t ) { if ( isset( $attachment[ $t ] ) ) { wp_set_object_terms( $attachment_id, array_map( 'trim', preg_split( '/,+/', $attachment[ $t ] ) ), $t, false ); } } } } if ( isset( $_POST['insert-gallery'] ) || isset( $_POST['update-gallery'] ) ) { ?> <script type="text/javascript"> var win = window.dialogArguments || opener || parent || top; win.tb_remove(); </script> <?php exit; } if ( isset( $send_id ) ) { $attachment = wp_unslash( $_POST['attachments'][ $send_id ] ); $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : ''; if ( ! empty( $attachment['url'] ) ) { $rel = ''; if ( strpos( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) == $attachment['url'] ) { $rel = " rel='attachment wp-att-" . esc_attr( $send_id ) . "'"; } $html = "<a href='{$attachment['url']}'$rel>$html</a>"; } /** * Filters the HTML markup for a media item sent to the editor. * * @since 2.5.0 * * @see wp_get_attachment_metadata() * * @param string $html HTML markup for a media item sent to the editor. * @param int $send_id The first key from the $_POST['send'] data. * @param array $attachment Array of attachment metadata. */ $html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment ); return media_send_to_editor( $html ); } return $errors; }
function media_upload_form_handler() {
	check_admin_referer( 'media-form' );

	$errors = null;

	if ( isset( $_POST['send'] ) ) {
		$keys    = array_keys( $_POST['send'] );
		$send_id = (int) reset( $keys );
	}

	if ( ! empty( $_POST['attachments'] ) ) {
		foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
			$post  = get_post( $attachment_id, ARRAY_A );
			$_post = $post;

			if ( ! current_user_can( 'edit_post', $attachment_id ) ) {
				continue;
			}

			if ( isset( $attachment['post_content'] ) ) {
				$post['post_content'] = $attachment['post_content'];
			}

			if ( isset( $attachment['post_title'] ) ) {
				$post['post_title'] = $attachment['post_title'];
			}

			if ( isset( $attachment['post_excerpt'] ) ) {
				$post['post_excerpt'] = $attachment['post_excerpt'];
			}

			if ( isset( $attachment['menu_order'] ) ) {
				$post['menu_order'] = $attachment['menu_order'];
			}

			if ( isset( $send_id ) && $attachment_id == $send_id ) {
				if ( isset( $attachment['post_parent'] ) ) {
					$post['post_parent'] = $attachment['post_parent'];
				}
			}

			/**
			 * Filters the attachment fields to be saved.
			 *
			 * @since 2.5.0
			 *
			 * @see wp_get_attachment_metadata()
			 *
			 * @param array $post       An array of post data.
			 * @param array $attachment An array of attachment metadata.
			 */
			$post = apply_filters( 'attachment_fields_to_save', $post, $attachment );

			if ( isset( $attachment['image_alt'] ) ) {
				$image_alt = wp_unslash( $attachment['image_alt'] );

				if ( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) !== $image_alt ) {
					$image_alt = wp_strip_all_tags( $image_alt, true );

					// update_post_meta() expects slashed.
					update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
				}
			}

			if ( isset( $post['errors'] ) ) {
				$errors[ $attachment_id ] = $post['errors'];
				unset( $post['errors'] );
			}

			if ( $post != $_post ) {
				wp_update_post( $post );
			}

			foreach ( get_attachment_taxonomies( $post ) as $t ) {
				if ( isset( $attachment[ $t ] ) ) {
					wp_set_object_terms( $attachment_id, array_map( 'trim', preg_split( '/,+/', $attachment[ $t ] ) ), $t, false );
				}
			}
		}
	}

	if ( isset( $_POST['insert-gallery'] ) || isset( $_POST['update-gallery'] ) ) {
		?>
		<script type="text/javascript">
		var win = window.dialogArguments || opener || parent || top;
		win.tb_remove();
		</script>
		<?php

		exit;
	}

	if ( isset( $send_id ) ) {
		$attachment = wp_unslash( $_POST['attachments'][ $send_id ] );
		$html       = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';

		if ( ! empty( $attachment['url'] ) ) {
			$rel = '';

			if ( strpos( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) == $attachment['url'] ) {
				$rel = " rel='attachment wp-att-" . esc_attr( $send_id ) . "'";
			}

			$html = "<a href='{$attachment['url']}'$rel>$html</a>";
		}

		/**
		 * Filters the HTML markup for a media item sent to the editor.
		 *
		 * @since 2.5.0
		 *
		 * @see wp_get_attachment_metadata()
		 *
		 * @param string $html       HTML markup for a media item sent to the editor.
		 * @param int    $send_id    The first key from the $_POST['send'] data.
		 * @param array  $attachment Array of attachment metadata.
		 */
		$html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );

		return media_send_to_editor( $html );
	}

	return $errors;
}

常见问题

FAQs
查看更多 >