解决WORDPRESS不能上传audio/mp3文件的问题

WP会验证上传文件的mime_types

从functions.php的wp_get_mime_types函数可以看出 WP默认MP3文件的mine为audio/mpeg

'mp3|m4a|m4b' => 'audio/mpeg',

但是MP3文件的mine还有 audio/mp3,上传时会提示”抱歉,出于安全的考虑,不支持此文件类型。”

而且在’audio/mpeg’后面加|audio/mp3,也不能通过WP的验证

优雅的方式不行,那么就硬来了

修改/wp-admin/include/file.php的_wp_handle_upload函数

	// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
	if ( $test_type ) {
		$wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
		$ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
		$type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
		$proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];

		// Check to see if wp_check_filetype_and_ext() determined the filename was incorrect
		if ( $proper_filename ) {
			$file['name'] = $proper_filename;
		}
		//这里添加额外规则
		if ( ( ! $type || !$ext ) && ! current_user_can( 'unfiltered_upload' ) && $file['type'] != 'audio/mp3') {
			return call_user_func_array( $upload_error_handler, array( &$file, __( 'Sorry, this file type is not permitted for security reasons.' ) ) );
		}
		if ( ! $type ) {
			$type = $file['type'];
		}
	} else {
		$type = '';
	}
	//把type修改为WP认识的
	if($type == 'audio/mp3') $type = 'audio/mpeg';

首先当mine为audio/MP3时,不允许WP报错

然后把audio/MP3替换成 audio/mpeg 这样在正文WP才会老老实实的加载出来