How to add SVG to WordPress?

Problem: By default, WordPress deny uploads svg files.

Allow upload SVG files to WordPress

Solution: As in most cases I proposal several options - install plugin or write code. WordPress has many plugins, who resolve this problem, e.g. this. If you prefer write code – welcome :)

add_filter( 'upload_mimes', 'allow_upload_svg' );

function allow_upload_svg( $mimes ) {
	$mimes['svg'] = 'image/svg+xml';
		
	return $mimes;
}

Pay attention, SVGs need to be sanitized! Sanitation is basically the cleaning of code or input to avoid security issues (such as code injection), code conflicts, and errors. For sanitize files you can use online solutions or different libraries (I think it's variant be preferable). In accordance with this path, you can allow other type files, who is deny in WordPress by default. List allowed file types, you can see using function:

get_allowed_mime_types();

Sources:

  1. https://kinsta.com/blog/wordpress-svg/
  2. https://codex.wordpress.org/Function_Reference/get_allowed_mime_types
Leave Comment

Your email address will not be published. Required fields are marked *