Uploads and limitseasy_svg_max_bytes

easy_svg_max_bytes

Set the largest SVG, in bytes, that Easy SVG will read on uploads, imports, XML-RPC and the icon library. Overrides the size from Icons, Settings.

Filter · Easy SVG · Status: Stable

Easy SVG refuses any SVG that is bigger than a set size before it even starts cleaning it. Site owners pick that size in megabytes under Icons → Settings. This filter lets your code decide the final number in bytes, for example a limit below 1 MB or a different limit per user.

Usage

apply_filters( 'easy_svg_max_bytes', int $bytes )

Applied in easy-svg.php, inside the internal function easy_svg_max_bytes(). The result is cast to an integer.

When to use it

  • You want a limit below 1 MB. The setting under Icons → Settings only accepts whole megabytes from 1 upwards, so 300 KB is only reachable through this filter.
  • You want a different limit for different people, for example a tight limit for authors and the full limit for administrators.
  • You manage many sites and want the limit to come from code or wp-config.php, not from a setting each admin can change.
  • You need a limit above the ceiling (20 MB by default) on one site and do not want to raise the ceiling for the settings field.

Do not use it when:

  • You only want admins to be able to pick a higher number in the settings field. Use easy_svg_max_mb_ceiling for that.
  • The problem is a file with thousands of use elements or many processing instructions. Size is not the issue there: see easy_svg_max_use_tags and easy_svg_max_php_tags.
  • Your server refuses the upload before WordPress sees it. PHP's own upload_max_filesize and post_max_size apply first, and this filter cannot raise them.

Parameters

body
$bytesint
Required

The limit that comes from the settings: the stored "Maximum SVG size (MB)" value times MB_IN_BYTES. With the default setting of 2 MB that is 2097152. The stored value is always between 1 and the ceiling, so without other filters this is at least 1048576.

Return value

returnint

The largest SVG, in bytes, that Easy SVG will read. A file is refused when its size is greater than this number, so a file of exactly this size passes. Return a positive integer. The value is cast with (int), so a string such as "500000" works, while null or false become 0 (see Pitfalls).

Default

max_mb * MB_IN_BYTES, where max_mb is the "Maximum SVG size (MB)" setting under Icons → Settings. A fresh install uses 2 MB, which is 2097152 bytes.

Examples

A limit below one megabyte

/**
 * Accept SVG files up to 300 KB.
 */
function mysite_svg_max_bytes( $bytes ) {
	return 300 * KB_IN_BYTES;
}
add_filter( 'easy_svg_max_bytes', 'mysite_svg_max_bytes' );

A smaller limit for everyone who is not an administrator

/**
 * Authors and editors get 512 KB, administrators keep the site setting.
 */
function mysite_svg_max_bytes_by_role( $bytes ) {
	if ( current_user_can( 'manage_options' ) ) {
		return $bytes;
	}

	return min( $bytes, 512 * KB_IN_BYTES );
}
add_filter( 'easy_svg_max_bytes', 'mysite_svg_max_bytes_by_role' );

min() keeps the smaller of the two numbers, so lowering the setting still works for everyone. Keep in mind that WP-CLI imports and cron jobs run without a logged in user and therefore get the smaller limit too.

The limit from wp-config.php, never zero

/**
 * In wp-config.php: define( 'MYSITE_SVG_MAX_KB', 750 );
 */
function mysite_svg_max_bytes_from_config( $bytes ) {
	if ( ! defined( 'MYSITE_SVG_MAX_KB' ) ) {
		return $bytes;
	}

	// Never return 0 or less: that would refuse every upload.
	return max( 1, (int) MYSITE_SVG_MAX_KB ) * KB_IN_BYTES;
}
add_filter( 'easy_svg_max_bytes', 'mysite_svg_max_bytes_from_config' );

Using the same limit in your own add-on

If your add-on accepts SVG markup itself, refuse the same files the site refuses. The function easy_svg_max_bytes() is internal, so check that it exists and fall back to the default.

function myaddon_svg_is_small_enough( $markup ) {
	$limit = function_exists( 'easy_svg_max_bytes' ) ? easy_svg_max_bytes() : 2 * MB_IN_BYTES;

	return strlen( (string) $markup ) <= $limit;
}

How it works

The filter runs every time the limit is needed. Nothing is cached, so a changed setting or a filter added late in the request takes effect at once. The callback gets no file, no path and no context, only the number.

These places read the limit:

WhereWordPress hookWhat happens when a file is too large
Media uploader, block editor, REST media uploadswp_handle_upload_prefilterUpload fails with "Sorry, please check your SVG file."
media_sideload_image(), WP-CLI wp media import, importerswp_handle_sideload_prefilterSame message
XML-RPC and plugins using wp_upload_bits()wp_upload_bits"This SVG file contains content this site does not allow. Please upload it through the media library, which cleans it."
Every new SVG attachment (safety net)add_attachmentThe attachment and its file are deleted, without a message
Width and height for the media library and Image blockwp_generate_attachment_metadata, wp_prepare_attachment_for_jsNo width and height are read, nothing is refused
Icon library: add, add from media library, replaceIcons panel REST routes"That SVG is larger than the size allowed in Settings." (replace: "That file is not an SVG this library can use.")

The upload, sideload and XML-RPC hooks are only registered while "Allow SVG uploads in the media library" is switched on. The add_attachment safety net always runs.

On an upload the order is: the file type is checked, then the size on disk is compared with the limit (the file is not read yet), then the file is read and its length compared again, then the structure check runs (easy_svg_max_php_tags, easy_svg_max_use_tags), and only then the sanitizer.

The setting and this filter work together like this: the stored megabytes are clamped to 1 up to the ceiling, multiplied by MB_IN_BYTES, then passed to this filter. The filter result is not clamped to the ceiling, so it can go higher or lower than anything the settings field allows.

Pitfalls

  • Zero means two different things. Return 0 (or null, false, a negative number) and every non empty upload is refused, but the icon library treats 0 and less as "no limit" and accepts any size there. Always return a positive number.
  • The panel does not know about your filter. The settings field still shows the stored megabytes and says "Larger files are refused. Allowed: 1 to 20 MB." If your filter lowers the limit, the screen is wrong for your site. Tell your editors.
  • One message for three reasons. In the media library a file that is too large, too complex or impossible to clean all get "Sorry, please check your SVG file." Nobody can tell from the message that size was the cause.
  • Imports can delete files. The safety net on add_attachment deletes a new SVG attachment and its file when it is over the limit. Lowering the limit before a big import means oversized SVGs are removed, not skipped with a notice.
  • Existing files lose their size information. Width and height are read on every media library listing with the same limit. Lower the limit and SVGs already in the library that are now too large show up without dimensions. They are not deleted.
  • The server limit comes first. PHP's upload_max_filesize and post_max_size stop larger uploads before WordPress runs. Raising this filter above them changes nothing for the uploader.
  • Contexts without a user. Role based callbacks see no user in WP-CLI, cron and some importers.
This limit is a safety bound: the sanitizer builds a full DOM of every file, so a very large SVG can keep one PHP worker busy and slow the site for everybody. Raise it only as far as you really need, and only for people you trust.