easy_svg_respect_network_filetypes
Decide whether a multisite network's Upload file types setting can keep SVG out of a site, even when that site has switched SVG uploads on in Easy SVG.
Filter · Easy SVG · Status: Stable
On a multisite network, the network admin decides in Upload file types which files any site may upload. Easy SVG follows that decision by default: if svg is not in the list, a single site cannot turn SVG uploads on for itself. This filter lets you change that rule, for all sites or only for some.
Usage
apply_filters( 'easy_svg_respect_network_filetypes', bool $respect )
add_filter( 'easy_svg_respect_network_filetypes', '__return_false' );
When to use it
- You manage the network and want SVG on a handful of sites without allowing it network-wide in Upload file types.
- You run a network where every site owner is trusted and the per-site switch in Easy SVG should be the only decision.
Do not use it:
- On a single site installation. The filter is never applied there.
- To allow SVG on every site of the network. Add
svgto Upload file types instead, so the network setting tells the truth. - To work around a network admin who left SVG out on purpose. That is their decision to make.
Parameters
true (the default) means the network list decides: SVG is only offered when Upload file types contains svg. false means the network list is ignored for SVG and the site's own Easy SVG switch decides. The value is used in a plain truthiness check, so 0, '' and null count as false.
Return value
true to honour the network's Upload file types list, false to add svg regardless of it.
Default
true. Introduced in Easy SVG 5.0.0. Before that, the plugin added svg to every site's allowed types, whatever the network allowed.
Examples
Let every site decide for itself
// mu-plugins/myplugin-svg-policy.php
add_filter( 'easy_svg_respect_network_filetypes', '__return_false' );
A must-use plugin is the natural home on a network: it loads on every site and no site admin can deactivate it.
Allow SVG only on selected sites
/**
* Ignore the network list for SVG on a few sites only.
*
* @param bool $respect Whether the network setting is honoured.
* @return bool
*/
function myplugin_svg_network_policy( $respect ) {
$sites_with_svg = array( 3, 7, 12 );
if ( in_array( get_current_blog_id(), $sites_with_svg, true ) ) {
return false;
}
return $respect;
}
add_filter( 'easy_svg_respect_network_filetypes', 'myplugin_svg_network_policy' );
The filter gets no site argument. It runs inside upload_mimes while WordPress handles a request for one site, so get_current_blog_id() is the site in question. Those sites still need Allow SVG uploads in the media library switched on in Easy SVG.
Change the network setting instead
When most sites should get SVG, edit the policy itself rather than overriding it. With WP-CLI:
# See what the network allows today (a space separated list).
wp site option get upload_filetypes
# Write the list back with svg appended. Keep every type that was there.
wp site option update upload_filetypes "$(wp site option get upload_filetypes) svg"
The same list is under Network Admin, Settings, Network Settings, Upload file types.
Returning false overrides a decision a network admin made on purpose. Every site that switches SVG uploads on then accepts SVG files, and each of those sites' upload permissions decides who can add them. Easy SVG still sanitizes every file, but the network admin no longer controls where SVG is possible.
How it works
The filter is applied in esw_add_support() (easy-svg.php, line 812), Easy SVG's callback on upload_mimes:
Details that follow from the code:
esw_add_support()is only hooked while the site's Allow SVG uploads in the media library setting is on. With the setting off, nothing addssvg, whatever this filter returns.- The setting is read when Easy SVG loads, so a change applies from the next request.
- The network option is lower-cased and split on whitespace, so
SVGandsvgboth count, butsvg,png(with a comma) does not containsvg. - Core's own multisite check runs on the same hook before Easy SVG, so when you return
falseEasy SVG addssvgafter core has filtered the list. That is why the override works. - Without
svgin the allowed types, an upload named.svgis refused by Easy SVG's upload check with "Sorry, this SVG file is not allowed for security reasons." esw_add_support()is pluggable. If a function with that name exists before Easy SVG loads, Easy SVG neither defines nor hooks its own, and this filter is never applied.
Pitfalls
- Single sites ignore it.
is_multisite()is checked first; on a single site the filter is not even called. - It does not switch SVG on. It only removes the network veto. The site still needs Easy SVG's upload setting.
- Commas break the network list. WordPress expects spaces in Upload file types. A comma separated value hides
svgfrom this check. - The icon library is not affected. Adding icons in Icons, Icon library does not go through
upload_mimes, so neither the network list nor this filter limits it. Icons have their own permission (administrators). - Sanitizing does not change. Whatever this filter returns, every SVG that reaches the media library is still cleaned, and the check on new attachments runs on every site where Easy SVG is active.