media_handle_upload() 主要用于 WordPress 上传文件,函数发送 POST 请求提交需要上传的文件并直接绑定文章,另外,函数默认返回上传文件的 id 或者 WP_Error 对象。该函数的语法如下:
media_handle_upload( $file_id, $post_id, $post_data, $overrides );
$file_id:(string) (必须的) 。PHP 中 $_FILES 数组的文件索引。默认:None。
$post_id:(int) (必需的) 。需要插入附件的文章 ID,如果不想把附件插入到文章里,设为 0 即可。默认: None。
$post_data:(array) (可选) 。允许修改附件的一些信息。默认: array()。
$overrides:(array) (可选)。 允许覆盖 wp_handle_upload() 的表现。默认: array( ‘test_form’ => false )。
该函数定义在 wp-admin/includes/media.php 文件中,具体代码如下:
function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) { $time = current_time( 'mysql' ); if ( $post = get_post( $post_id ) ) { // The post date doesn't usually matter for pages, so don't backdate this upload. if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) { $time = $post->post_date; } } $file = wp_handle_upload( $_FILES[ $file_id ], $overrides, $time ); if ( isset( $file['error'] ) ) { return new WP_Error( 'upload_error', $file['error'] ); } $name = $_FILES[ $file_id ]['name']; $ext = pathinfo( $name, PATHINFO_EXTENSION ); $name = wp_basename( $name, ".$ext" ); $url = $file['url']; $type = $file['type']; $file = $file['file']; $title = sanitize_text_field( $name ); $content = ''; $excerpt = ''; if ( preg_match( '#^audio#', $type ) ) { $meta = wp_read_audio_metadata( $file ); if ( ! empty( $meta['title'] ) ) { $title = $meta['title']; } if ( ! empty( $title ) ) { if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) { /* translators: 1: audio track title, 2: album title, 3: artist name */ $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] ); } elseif ( ! empty( $meta['album'] ) ) { /* translators: 1: audio track title, 2: album title */ $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] ); } elseif ( ! empty( $meta['artist'] ) ) { /* translators: 1: audio track title, 2: artist name */ $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] ); } else { /* translators: 1: audio track title */ $content .= sprintf( __( '"%s".' ), $title ); } } elseif ( ! empty( $meta['album'] ) ) { if ( ! empty( $meta['artist'] ) ) { /* translators: 1: audio album title, 2: artist name */ $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] ); } else { $content .= $meta['album'] . '.'; } } elseif ( ! empty( $meta['artist'] ) ) { $content .= $meta['artist'] . '.'; } if ( ! empty( $meta['year'] ) ) { /* translators: Audio file track information. %d: Year of audio track release */ $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] ); } if ( ! empty( $meta['track_number'] ) ) { $track_number = explode( '/', $meta['track_number'] ); if ( isset( $track_number[1] ) ) { /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks */ $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) ); } else { /* translators: Audio file track information. %s: Audio track number */ $content .= ' ' . sprintf( __( 'Track %s.' ), number_format_i18n( $track_number[0] ) ); } } if ( ! empty( $meta['genre'] ) ) { /* translators: Audio file genre information. %s: Audio genre name */ $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] ); } // Use image exif/iptc data for title and caption defaults if possible. } elseif ( 0 === strpos( $type, 'image/' ) && $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'] ) ) { $excerpt = $image_meta['caption']; } } // 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_excerpt' => $excerpt, ), $post_data ); // This should never be set as it would then overwrite an existing attachment. unset( $attachment['ID'] ); // Save the data $id = wp_insert_attachment( $attachment, $file, $post_id, true ); if ( ! is_wp_error( $id ) ) { wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); } return $id; }
参考文档:https://developer.wordpress.org/reference/functions/media_handle_upload/
评论 (1)