让WordPress 4.9支持上传更多文件类型

WordPress对上传的文件类型是有限制的,在这里,我们以在子主题twentyfifteen-child中增加扩展名为.bz2的文件类型为例子。

首先编辑子主题的functions.php文件

$ sudo vim /var/www/wordpress/wp-content/themes/twentyfifteen-child/functions.php

在文件尾部增加如下内容:

<?php
	/*BUG Fix for after wordpress https://core.trac.wordpress.org/ticket/21299*/
	add_filter( 'wp_check_filetype_and_ext', 'upload_mimes_fix_after_47', 10, 4 );
	function upload_mimes_fix_after_47($data, $file, $filename, $mimes) {
		global $wp_version;
		if( $wp_version == '4.7' || ( (float) $wp_version < 4.7 ) ) { return $data; }
		$filetype = wp_check_filetype( $filename, $mimes );
		return [ 'ext' => $filetype['ext'], 'type' => $filetype['type'], 'proper_filename' => $data['proper_filename'] ];
	}

	/*add upload file type*/
	add_filter('upload_mimes', 'custom_upload_mimes');
	function custom_upload_mimes ( $existing_mimes=array() ) {
		$existing_mimes['bz2'] = 'application/octet-stream';
		return $existing_mimes;
	}
?>

参考链接