如何实现WordPress站点媒体库支持上传SVG文件?

WordPress国产主题推荐

WordPress站点媒体库默认情况下是不允许上传SVG文件的,那么要怎么做才能实现WordPress站点媒体库支持上传SVG文件呢?其实,操作很简单,只需要将以下代码添加到当前主题的functions.php文件中并保存更新文件即可。

//WordPress媒体库允许上传SVG文件
add_filter(
	'upload_mimes',
	function ( $upload_mimes ) {
		if ( ! current_user_can( 'administrator' ) ) {
			return $upload_mimes;
		}

		$upload_mimes['svg']  = 'image/svg+xml';
		$upload_mimes['svgz'] = 'image/svg+xml';

		return $upload_mimes;
	}
);
add_filter(
	'wp_check_filetype_and_ext',
	function ( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) {

		if ( ! $wp_check_filetype_and_ext['type'] ) {

			$check_filetype  = wp_check_filetype( $filename, $mimes );
			$ext             = $check_filetype['ext'];
			$type            = $check_filetype['type'];
			$proper_filename = $filename;

			if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
				$ext  = false;
				$type = false;
			}

			$wp_check_filetype_and_ext = compact( 'ext', 'type', 'proper_filename' );
		}

		return $wp_check_filetype_and_ext;

	},
	10,
	5
);

以上代码来自@WPCode插件

文章创作不易,期待您的评分

本文地址:https://boke112.com/article/p11481.html

版权声明:本文内容来源于互联网资源,由 boke112百科 整理汇总!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
wu