EASY_SVG_API
Version number of the sanitizer contract in Easy SVG. Check it instead of the plugin version before your add-on calls easy_svg_sanitizer().
Constant · Easy SVG · Status: Stable
EASY_SVG_API is a plain number that tells add-ons which version of Easy SVG's sanitizer contract is installed. The contract is one function, easy_svg_sanitizer(), which returns a sanitizer set up exactly the way the site cleans SVG. When the constant exists, you can use that function. Since 5.0.0.
Usage
define( 'EASY_SVG_API', 3 );
Defined in easy-svg.php, at the moment the plugin file loads. Read it with defined( 'EASY_SVG_API' ) and compare it as an integer.
When to use it
- Your plugin or theme cleans SVG markup and should use the same rules as the site (the allow-list from
esw_svg_allowed_tagsandesw_svg_allowed_attributes, and the remote reference setting). - You want to know whether Easy SVG is installed and offers that function, without comparing plugin versions.
Do not use it when:
- You need the Icons panel extension points. That is
EASY_SVG_PANEL_API. - You need the icon write functions or
easy_svg_registrable_icons. That isEASY_SVG_ICONS_API.
Parameters
None: this is a constant, not a function.
Return value
Today 3. The number changes only when the shape of easy_svg_sanitizer() changes, not with every release. That makes it safer to compare than the plugin version: two plugin versions with and without a given feature can look the same to a version comparison, while this number cannot.
Default
Not applicable: the constant is defined by the plugin and is missing when Easy SVG is not active.
Examples
Clean markup the way the site does
function myaddon_clean_svg( $markup ) {
if ( ! defined( 'EASY_SVG_API' ) || EASY_SVG_API < 3 ) {
return false;
}
$sanitizer = easy_svg_sanitizer();
if ( null === $sanitizer ) {
return false; // The sanitizing library is not loaded.
}
$clean = $sanitizer->sanitize( (string) $markup );
return is_string( $clean ) && '' !== trim( $clean ) ? $clean : false;
}
Check once, after all plugins have loaded
Whether EASY_SVG_API exists while your own plugin file loads depends on load order. Decide on plugins_loaded.
function myaddon_maybe_boot() {
if ( ! defined( 'EASY_SVG_API' ) ) {
add_action( 'admin_notices', 'myaddon_missing_easy_svg_notice' );
return;
}
require_once __DIR__ . '/includes/svg-features.php';
}
add_action( 'plugins_loaded', 'myaddon_maybe_boot' );
function myaddon_missing_easy_svg_notice() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
printf(
'<div class="notice notice-warning"><p>%s</p></div>',
esc_html__( 'My Add-on needs Easy SVG Support to be active.', 'myaddon' )
);
}
Defensive: constant and function
$myaddon_can_sanitize = defined( 'EASY_SVG_API' ) && function_exists( 'easy_svg_sanitizer' );
How it works
define( 'EASY_SVG_API', 3 )runs at the top ofeasy-svg.php, right after the two allow-list classes and before any settings are read. Once Easy SVG has loaded, the constant exists on every request: front end, admin, REST, cron and WP-CLI.- It is defined whether or not SVG uploads or the icon library are switched on. It says that the code is there, not that a feature is enabled.
easy_svg_sanitizer()returns a new sanitizer on every call, configured with the filtered allow-lists andesw_svg_remove_remote_references. It returnsnullwhen the sanitizing library is not available.- WordPress loads active plugins in the order of their file paths. A plugin in a folder that sorts before
easy-svg(for exampleeasy-svg-pro, because-sorts before/) and all must-use plugins load earlier, so the constant is not there yet while their main file runs.
Pitfalls
- Checking too early. At the top of your plugin file the constant can be missing even though Easy SVG is active. Check on
plugins_loadedor later. - Comparing plugin versions instead. The readme promises this number, not version strings.
- Assuming
easy_svg_sanitizer()returns an object. It can returnnull. Always check before callingsanitize(). - Using the classes behind it.
esw_svg_tags,esw_svg_attributesand the global$sanitizerare internal. A rename makesclass_exists()false and your add-on would think Easy SVG is missing.