wordpress添加允许/拒绝某个文件类型的上传

主要就是利用upload_mimes过滤钩子,进行添加、修改、删除,代码放到主题functions.php或者放到插件里面.

例子1:

function my_custom_mime_types( $mimes ) {
	
	// New allowed mime types.
	$mimes['svg']  = 'image/svg+xml';
	$mimes['svgz'] = 'image/svg+xml';
	$mimes['doc']  = 'application/msword'; 

    // Optional. Remove a mime type.
    unset( $mimes['exe'] );

	return $mimes;
}

add_filter( 'upload_mimes', 'my_custom_mime_types' );

例子2:

<?php
function svgz_mime_types( $mimes ) {
        // SVGZ allowed mime types.
        $mimes['svgz'] = 'application/x-gzip';
        return $mimes;
}
add_filter( 'upload_mimes', 'svgz_mime_types' );
?>

例子3:

<?php
/**
 * Allow JSON file uploads to WordPress media library.
 *
 * Filters the uploads mime types to allow JSON files.
 *
 * @param array $types Currently allowed types.
 *
 * @return array Filtered types.
 */
add_filter(
	'upload_mimes',
	function( $types ) {
		return array_merge( $types, array( 'json' => 'text/plain' ) );
	}
);

例子4:

function extend_mime_types( $types ) {
	$types['csv'] = 'text/csv';
	return $types;
}
add_filter( 'upload_mimes', 'extend_mime_types' );

例子5:

function wpdocs_allow_mime_types( $mimes ) {

		$mimes['ttf']   = 'font/ttf';
		$mimes['woff']  = 'font/woff';
		$mimes['woff2'] = 'font/woff2';
	return $mimes;
}

add_filter( 'upload_mimes', 'wpdocs_allow_mime_types' );

例子6:

// To upload photos in format WebP

function wpdocs_custom_mime_types( $mimes ) {
	
	// New allowed mime types.
	$mimes['webp'] = 'image/webp';

	return $mimes;
}

add_filter( 'upload_mimes', 'wpdocs_custom_mime_types' );
© 版权声明
WWW.ANXKJ.TOP
喜欢就支持一下吧
点赞12 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容